Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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 - Fatal编程技术网

Php 如何从文本文件中的行动态创建文件

Php 如何从文本文件中的行动态创建文件,php,Php,如何使用PHP在文本文件中循环并创建每行文本的基础文件xxx.txt 例如,我有一个名为files.txt的文件,它包含 files.txt red pink purple deep purple indigo blue light blue cyan teal green 我已经试过了 <?php $file = fopen("files.txt","r"); while(! feof($file)) { echo fgets($file). "<b

如何使用PHP在文本文件中循环并创建每行文本的基础文件
xxx.txt

例如,我有一个名为
files.txt
的文件,它包含

files.txt

red 
pink 
purple 
deep purple 
indigo 
blue 
light blue 
cyan 
teal 
green 
我已经试过了

<?php
$file = fopen("files.txt","r");

while(! feof($file))
  {
  echo fgets($file). "<br />";
  $fileName = fgets($file);
  $myfile = fopen($fileName.'.txt, "w");

  }

fclose($file);
?> 

请参见:,您想做什么?是否为文件中的每个值创建一个文件?写些什么?为什么不用?看在上帝的份上,你为什么否决他的问题?!
$handle = fopen("path/to/files.txt", "r");
$filesToCreate = [];
if ($handle) {
    while (($line = fgets($handle)) !== false) {

        // you may need to add the full path here..
        $filesToCreate[] = $line . '.txt';
    }

    fclose($handle);
} else {
    // error opening the file.
}

foreach ($filesToCreate as $newFile) {
    $createdFile = fopen($newFile, 'w');
    fclose($createdFile);
}