1

How to write in between content of existing file?

I tried to use fseek() to seek a new position and write the new content, but it replaces old content to the new string after seek position.

My aim is to put new contents after 5 characters of existing file.

Old content: AAAAABBBBB, desired content: AAAAAnewcontentBBBBB

$file_handler = fopen('putty.log','w');
$new_content = 'this is new content';

fseek($file_handler,5);

echo ftell($file_handler); //5
fwrite($file_handler,$new_content);

old content replaced with NULLNULLNULLNULLNULLthis is new content

1

4 Answers 4

1

You can't do it that way.

You can only truncate the content with ftruncate and write later the old content

A not so clean example

<?php

$file_handler = fopen('putty.log','rb+');
$new_content = 'this is new content';

fseek($file_handler,5);

$restOfContent = fread($file_handler,filesize('putty.log')-5);

ftruncate($file_handler,5);
fseek($file_handler,5);

echo ftell($file_handler); //5
fwrite($file_handler,$new_content);

fwrite($file_handler,$restOfContent);

fclose($file_handler)
Sign up to request clarification or add additional context in comments.

2 Comments

don't we need to rewind() after fread($file_handler,filesize('putty.log')-5);? or after reading EOF if we seek again it starts from beginning?
i think when file pointer reaches EOF and i seek again, it starts from 0.
1
  1. Load contents to variable using file_get_contents().
  2. Do this on your buffer.
  3. Save your contents to this file using file_put_contents();

2 Comments

yup that's memory burden if file is huge.
yes but fastest development ;-) think about how code will be used in lifetime, because maybe this can be better solution in relation to your time what you spend on that
1

if you're using fseek() to write data to a file, remember to open the file in "r+" mode, example:

$fp=fopen($filename,"r+");

2 Comments

thanks, i guess w mode always truncate file to zero length in beginning.
w actually overwrites the existing file.
1

Writing in the middle of the file won't cause it to stretch and push the content forward (like when inserting content in a text editor) but rather to overwrite the content at the position you start writing. What you need to do is:

  1. read the first part of the old data until the point where you need to write the new content & write that part to a temporary file.
  2. write the new content to the temporary file (the stuff you wish to add).
  3. read the rest of the content from the old file & write it to the temporary file.
  4. delete the old file.
  5. rename the temporary file to the name of the old file.

Example:

$original_file_name = '/tmp/putty.log';
$temp_file_name = '/tmp/putty.tmp';
$temp_file = fopen( $temp_file_name, 'w' );
$file_handler = fopen( $original_file_name, 'r' );
$old_data_size = 5;
fwrite( $temp_file, fread( $file_handler, $old_data_size ) );
$new_content = 'this is new content';
fwrite( $temp_file, $new_content, strlen( $new_content ) );
fwrite( $temp_file, fread( $file_handler, filesize( $original_file_name ) - $old_data_size ) );
fclose( $file_handler );
fclose( $temp_file );
unlink( $original_file_name );
rename( $temp_file_name, $original_file_name );

Make sure that putty.log has read/write permissions for the user used by your webserver (apache/lighttpd etc.) process or that it's accessible for everyone (not recommended).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.