Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP创建具有特定名称的文件_Php_File_Filenames_Fopen - Fatal编程技术网

PHP创建具有特定名称的文件

PHP创建具有特定名称的文件,php,file,filenames,fopen,Php,File,Filenames,Fopen,创建新文件的以下代码运行良好: <?php $myfile = fopen("LMS-NUMBER.txt", "w") or die("Unable to open file!"); $txt = "John Doe\n"; fwrite($myfile, $txt); $txt = "Jane Doe\n"; fwrite($myfile, $txt); fclose($myfile); ?> 尽管在评论中指出,您的问题不清楚,但我认为我理解您想要做什么,尽管如前所述,您应该编

创建新文件的以下代码运行良好:

<?php
$myfile = fopen("LMS-NUMBER.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

尽管在评论中指出,您的问题不清楚,但我认为我理解您想要做什么,尽管如前所述,您应该编辑问题以使其更清楚

如果我是对的,你要问的是如何创建一个新文件,其数字后缀比以前的文件高一个,以防止覆盖。一种简单的方法是使用for()循环检查核心文件名+计数是否已经存在,并继续执行,直到找到一个不存在的循环。然后,您可以将文件名与循环的迭代一起存储在文件不存在的地方,最后使用该名称写入新文件。例如,

<?php
    /* Here you can set the core filename (from your question, "LMS"),
    as well as the number of maximum files. */
    $coreFileName   = "LMS";
    $maxFilesNum    = 100;

    // Will store the filename for fopen()
    $filename = "";

    for($i = 0; $i < $maxFilesNum; $i++)
    {
        // File name structure, taken from question context
        $checkFileName = $coreFileName . "-" . str_pad($i, 3, 0, STR_PAD_LEFT) . ".txt";

        // If the file does not exist, store it in $filename for writing
        if(!file_exists($checkFileName))
        {
            $filename = $checkFileName;
            break;
        }
    }

    $fd = fopen($filename, "w");
    fwrite($fd, "Jane Doe"); // Change string literal to the name to write to file from either input or string literal
    fclose($fd); // Free the file descriptor

例如:
$name=sprintf(“LMS-%03d.txt”,$number)
fopen('LMS-001.txt','w')
您能更具体地说明您想要什么吗?如果您想创建一个名为
LMS-001.txt
的文件,那么很容易看到您只是将“newfile.txt”替换为“LMS-001.txt”,但当我想创建名为:LMS-001.txt、LMS-002.txt、LMS-003.txt等的文件时?文件名可以是LMS-1.txt、LMS-2.txt、LMS-3.txt。但是什么时候呢?你的问题似乎到此为止了。你到底有什么问题?我需要通过一个表单提交创建更多文件。所以重复点击提交按钮后,会创建如下文件:LMS-1.txt、LMS-2.txt、LMS-3.txt。。。第一次单击=LMS-1.txt;第二次点击=LMS-2.txt;第三次点击=LMS-3.txt;对不起,我的英语太差了!非常感谢。可以创建如下文件名:LMS-001.txt,LMS-002.txt。。。LMS-099.txt,LMS-100.txt?@Tlumic为此,您可以简单地使用str_pad()在左边填充前导0,直到达到数百,因此,作为一个示例,您可以在For()循环中的变量$i上使用它:
str_pad($i,3,0,str_pad_left)
,如果您想看到它的运行,我已经将它编辑到了答案中。另外,请确保通过单击我的答案旁边的绿色复选框图像将问题标记为已回答:)绿色复选框将100%归您所有;)请编辑你的答案,干得好!太好了,再次谢谢你!
<?php
    /* Here you can set the core filename (from your question, "LMS"),
    as well as the number of maximum files. */
    $coreFileName   = "LMS";
    $maxFilesNum    = 100;

    // Will store the filename for fopen()
    $filename = "";

    for($i = 0; $i < $maxFilesNum; $i++)
    {
        // File name structure, taken from question context
        $checkFileName = $coreFileName . "-" . str_pad($i, 3, 0, STR_PAD_LEFT) . ".txt";

        // If the file does not exist, store it in $filename for writing
        if(!file_exists($checkFileName))
        {
            $filename = $checkFileName;
            break;
        }
    }

    $fd = fopen($filename, "w");
    fwrite($fd, "Jane Doe"); // Change string literal to the name to write to file from either input or string literal
    fclose($fd); // Free the file descriptor