tags:

views:

160

answers:

7

hey Guys,

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 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 :)

+1  A: 

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
Guillaume Flandre
+3  A: 

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.

Davide Gualano
+1  A: 
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); 
    }
}
Peter Lindqvist
A: 

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".

Alexandru Mos
This will do open/close for file in the loop, which is not good idea. It more efficient to open file before loop and close it after.
Ivan Nevostruev
A: 

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.

Alex
A: 

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";
}
?>
Per Wiklander
+2  A: 

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.

Saulius Lukauskas