Bir Metin Dosyasına Yazma (PHP)

7 Cevap

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.

İşte benim kod:

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

?>

Peki yerine sonuç yankılanan, ben daha ziyade bir metin dosyasına yazılı olurdu. Ayrıca, eğer mümkünse, sonuç yankı ve bir metin dosyasına yazma. plz bana bu teşekkür başarmak için ne ekleyebilirsiniz kod bildirin :)

7 Cevap

Ile deneyin:

<?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 dosyaya yazma hakkında daha fazla bilgi için ayrıca bkz.

Ben aslında bir döngü içinde IO işlemlerini olmaması tercih ediyorum:

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

Ayrıca, bazı kod kokuyor kurtuldum yerine - "Array"> "dizi" de sonunda "== true" (orada gerçek bir boolean karşılaştırma hiçbir nokta da, bu öyle ya da değil .)

Son olarak, pathinfo() uzantısını karşılaştırmak için kullanın.

Tüm ihtiyacınız şu olmalıdır:

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

Örnek kodla Uygulanan

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

Değiştir:

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

ile:

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

Bu "your_text_file_name.txt" içine yeni bir satır için her maç bir satır ekler.

Kod birkaç satır ile komut dosyasını değiştirmeniz gerekiyor.

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

Daha fazla bilgi için fopen() fonksiyonuna bakın. ) (Fopen için mümkün modları listesine bir göz atın. Sizin durumunuza en uygun bulabilirsiniz.

http://php.net her zaman bu tarz sorulara için iyidir.

İşte fwrite () fonksiyonu belgelerine "Örnek 1. Basit bir fwrite () örnek" bir kopyasıdır

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