写入文本文件(PHP)

时间:2009-12-07 15:41:14

标签: php

我知道这个问题可能是直截了当的,但我是php和编程的新手...... 我想将递归搜索的结果写入文本文件,并确保执行的任何其他搜索不会覆盖现有数据,而是添加到其中。

这是我的代码:

 <?php 

$it = new RecursiveDirectoryIterator("L:\folder\folder\folder"); 
$display = Array ( 'jpeg', 'jpg', 'tif', 'tiff', 'bmp', 'shp', 'gif', 'png' ); 
foreach(new RecursiveIteratorIterator($it) as $file) 
{ 
    if ( In_Array ( SubStr ( $file, StrrPos ( $file, '.' ) + 1 ), $display ) == true )
    { 
        echo $file . "<br/> \n"; 
    }
}

?>

因此,我宁愿将其写入文本文件,而不是回显结果。此外,如果可能,请回显结果并将其写入文本文件。请告诉我我可以添加哪些代码来实现这一点,谢谢:)

7 个答案:

答案 0 :(得分:3)

尝试:

<?php 

$it = new RecursiveDirectoryIterator("L:\folder\folder\folder"); 
$display = Array ( 'jpeg', 'jpg', 'tif', 'tiff', 'bmp', 'shp', 'gif', 'png' ); 

$fp = fopen("output.txt", "a");

foreach(new RecursiveIteratorIterator($it) as $file) 
{ 
    if ( In_Array ( SubStr ( $file, StrrPos ( $file, '.' ) + 1 ), $display ) == true )
    { 
        //echo $file . "<br/> \n"; 
        fwrite($fp, $file."\n");
    }
}

fclose($fp);
?>

有关写入文件的详细信息,另请参阅http://www.php.net/manual/en/function.fwrite.php

答案 1 :(得分:2)

我实际上更喜欢不在循环中进行IO操作:

$display = array ( 'jpeg', 'jpg', 'tif', 'tiff', 'bmp', 'shp', 'gif', 'png' ); 

$it = new RecursiveDirectoryIterator("L:\folder\folder\folder"); 

$output = null;

foreach(new RecursiveIteratorIterator($it) as $file) 
{ 
    if (in_array ( pathinfo($file, PATHINFO_EXTENSION), $display ))
    { 
        echo $file . "<br/> \n";
        $output .= $file."\n"; 
    }    
}

file_put_contents("output.txt", $output, FILE_APPEND);

另外,我摆脱了一些代码味道 - &gt;最后是“数组”而不是“数组”以及“== true”(没有必要将布尔值与真值进行比较,无论是它还是不是)。

最后,使用pathinfo()来比较扩展名。

答案 2 :(得分:1)

您需要的只是以下内容:

$myFile = "file.txt"; // name of the file
$fh = fopen($myFile,'a') or die("Can't open file"); // opens the file in "append" mode
fwrite($fh,$file); // write to the end of the file
fclose($fh); // close the file

答案 3 :(得分:1)

file_put_contents($filename,$data);

应用于您的示例代码

 <?php 

$it = new RecursiveDirectoryIterator("L:\folder\folder\folder"); 
$display = Array ( 'jpeg', 'jpg', 'tif', 'tiff', 'bmp', 'shp', 'gif', 'png' ); 
foreach(new RecursiveIteratorIterator($it) as $file) 
{ 
    if ( In_Array ( SubStr ( $file, StrrPos ( $file, '.' ) + 1 ), $display ) == true )
    { 
        file_put_contents('output.txt',$file . "\n",FILE_APPEND); 
    }
}

答案 4 :(得分:0)

变化:

echo $file . "<br/> \n";

使用:

file_put_contents('your_text_file_name.txt', $file."\n", FILE_APPEND);

这会将每个匹配行追加到“your_text_file_name.txt”的新行。

答案 5 :(得分:0)

您需要使用更多代码行修改脚本。

<?php 

$fhandler = fopen('file.txt', 'a'); // Flag 'a' prevents your script from deleting  
                                    // previous data from file 

$it = new RecursiveDirectoryIterator("L:\folder\folder\folder"); 
$display = Array ( 'jpeg', 'jpg', 'tif', 'tiff', 'bmp', 'shp', 'gif', 'png' ); 
foreach(new RecursiveIteratorIterator($it) as $file) 
{ 
    if ( In_Array ( SubStr ( $file, StrrPos ( $file, '.' ) + 1 ), $display ) == true )
    { 
        fwrite($fhandler, $file);  // Writing fileName to the file
        echo $file . "<br/> \n"; 
    }
}

fclose($fhandler);
?>

有关详细信息,请参阅fopen()函数。看一下fopen()的可能模式列表。你可以找到最适合你情况的。

答案 6 :(得分:0)

http://php.net总是适合这类问题。

这是fwrite()函数文档中的“Example#1 a simple fwrite()example”的副本

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>
相关问题