开发者

fwrite writing from beginning without removing

开发者_开发技巧

I am using PHP and fwrite code, but I want every write position to start from the beginning of the file without erasing it's content. I am using this code but it is writing to the end of the file.

$fr = fopen("aaaa.txt", "a");
fwrite($fr, "text");
fclose($fr);


So you want to write at the beginning of a file, leaving all of its current contents after the new data? You'll have to grab its existing contents first, then append it back into the file once you've overwritten with your new data.

$file = 'aaaa.txt';
$oldContents = file_get_contents($file);
$fr = fopen($file, 'w');
fwrite($fr, "text");
fwrite($fr, $oldContents);
fclose($fr);

If you want to avoid loading the original file's contents into memory in your PHP script, you might try first writing to a temp file, using a loop buffer or system calls to append the contents of your original file to the temp file, then remove the original file and rename your temp file.


From the PHP site:

Note:

If you have opened the file in append ("a" or "a+") mode, any data you write to the file will always be appended, regardless of the file position

I suggest something like this (from php.net site):

$handle = fopen('output.txt', 'r+');

fwrite($handle, 'Really long sentence.');
rewind($handle);
fwrite($handle, 'Foo');
rewind($handle);

echo fread($handle, filesize('output.txt'));

fclose($handle);


$file = 'aaaa.txt';
$tmp = file_get_contents($file);
$tmp = 'text'.$tmp;
$tmp = file_put_contents($file, $tmp);
echo ($tmp != false)? 'OK': '!OK';


use this code :

$file = 'aaaa.txt';
$oldContents = file_get_contents($file);
$fr = fopen($file, 'w');
$newmsg="text".$oldContents;
fwrite($fr, $oldContents);
fclose($fr);


Prefetching old content with file_get_contents and appending it after writing new content is one way to do it. But In case, the file is being dynamically written and along the way you needed to go back to the beginning of file and add some text, then here is how:

$handle = fopen($FILE_PATH, 'w+');
fwrite($handle, "I am writing to a new empty file 
                and now I need to add Hello World to the beginning");

to prepend Hello World do the following:

$oldText = '';
fseek($handle, 0);
while (!feof($handle)) {
   $oldText .= fgets($handle);
}
fseek($handle, 0);
fwrite($handle, "Hello World! ");
fwrite($handle, $oldText);

fclose($handle);

The result would be:

Hello World! I am writing to a new empty file and now I need to add Hello World to the beginning

Reminder and like Fabrizio already noted:

If you have opened the file in append ("a" or "a+") mode, any data you write to the file will always be appended, regardless of the file position


This should work:

$file="name.extension";
$current = file_get_contents($file);
$user = $_POST["username"];
$pass = $_POST["password"];
file_put_contents($file,$current . "Whatever you want to add here"

This finds the current stuff and copy/pastes it back each time the code is ran (tried to make it as simple as possible in case other answers are a little too complicated)


  1. Open a file in w+ mode not a+ mode.
  2. Get the length of text to add ($chunkLength)
  3. set a file cursor to the beginning of the file
  4. read $chunkLength bytes from the file
  5. return the cursor to the $chunkLength * $i;
  6. write $prepend
  7. set $prepend a value from step 4
  8. do these steps, while EOF

    $handler = fopen('1.txt', 'w+');//1
    rewind($handler);//3
    $prepend = "I would like to add this text to the beginning of this file";
    $chunkLength = strlen($prepend);//2
    $i = 0;
    do{
        $readData = fread($handler, $chunkLength);//4
        fseek($handler, $i * $chunkLength);//5
        fwrite($handler, $prepend);//6
    
        $prepend = $readData;//7
        $i++;
    }while ($readData);//8
    
    fclose($handler);
    


First open the file in c+ mode which opens file or create new file if doesnot exists and points the pointer at beginning of the file. To get old content use file_get_contents and check if the file exists.

$fh = fopen($file_path, 'c+'); 

if (file_exists($file_path)) {   
    $oldContents = file_get_contents($file_path);  
    fwrite($fh,"New Content" );  
    fwrite($fh, $oldContents);   
} else {   
    fwrite($fh,"New Content");   
}   

fclose($fh);


Use fseek() to set your position in the file.

$fr = fopen("aaaa.txt", "r+");
fseek($fr, 0); // this line will set the position to the beginning of the file
fwrite($fr, "text");
fclose($fr);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜