2011-07-24 6 views

Répondre

6

Yes it is.

$file = "./test.txt"; 
// open file at the beginning. 
$fh = fopen($file, 'r+'); 
//read the first line of the file. (advances pointer to the second line). 
$contents = fread($fh); 
// modify contents. 
$new_contents = str_replace("hello world", "hello", $contents); 
// make sure you're back at the 0 index. 
fseek($file, 0); 
// write 
fwrite($fh, $new_contents); 
// close. 
fclose($fh); 
// done! 
+1

Était trop paresseux pour écrire un échantillon complet. +1 à vous alors ;-) – zerkms

0

En PHP 5 file_put_contents est la meilleure façon:

<?php 
$file  = 'people.txt'; 
$current = file_get_contents($file); // Open the file to get existing content 
$current .= "John Smith\n";   // Append a new person to the file 
file_put_contents($file, $current); // Write the contents back to the file 
?> 
+0

Je trouve toujours amusant que file_get_contents ait existé plus longtemps que file_put_contents. – cwallenpoole

-1
file_put_contents("write.txt",file_get_contents("read.txt"));