开发者

Writing to a Text File (PHP)

I know this question is probably straightfoward but I'm new to php and programming... I want to write the results of a recursive search to a text file, and also ensuring that any other search开发者_Go百科 performed will not overwrite the exisiting data, but add to it.

here is my code:

 <?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"; 
    }
}

?>

So instead of echoing the result, I'd rather it be written to a text file. Also, if it is possible, echo the result and write it to a text file. plz let me know what code I can add in to achieve this thanks :)


Try with:

<?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);
?>

See also http://www.php.net/manual/en/function.fwrite.php for more info about writing to file.


I'd actually prefer not having IO operations in a loop:

$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);

Also, I got rid of some of the code smells -> "array" instead of "Array" as well as "== true" in the end (there's no point of comparing a boolean to true, either it is or it is not).

Finally, use pathinfo() To compare extension.


All you need should be in the following:

$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


file_put_contents($filename,$data);

Applied to your example code

 <?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); 
    }
}


Change:

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

with:

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

This will append every match row to a new line into "your_text_file_name.txt".


You need to modify your script with few more lines of code.

<?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);
?>

See fopen() function for more details. Take a look on the list of possible modes for fopen(). You can find most appropriate for your situation.


http://php.net is always good for these kinds of questions.

Here is a copy of "Example #1 A simple fwrite() example" from the documentation on the fwrite() function

<?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";
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜