简体   繁体   中英

How do you insert a line into a file at a specific line number?

In PHP there is fopen('r+') for Read/Write. (start at beginning of the file) There is also fopen('a+') for Read/Write. (append to the end of the file)

I made a simple $_POST['data'] form that writes whatever is written in the form to a file. My problem is that if I write a few things, for example.

'Red ' 
'Green ' 
'Blue '

If I append this data, it will show up in the file in the order I write in.

But, I need to write to the beginning of the file, so that it's in reverse order.

'Blue '
'Green '
'Red '

$file_data = 'What to write in the file';

$file_data .= file_get_contents('testz.php'); file_put_contents('testz.php', $file_data);

  1. This will write whatever is in the $file_data variable to the file and then write the data that was originally in the file afterwards.

Thus causing it to show up before everything else that was originally in the file. This works fine.


The problem starts when I want to include a line of code at the beginning of the file.

Specifically I want '!DOCTYPE html html head' to always be at the top of the document. I could of course put that in the file data like: file_put_contents('testz.php', $headerinformation.$file_data);

But the problem with that is gonna be everytime I submit the POST, It's gonna be repeating

<!DOCTYPE html><html><head>Blue
<!DOCTYPE html><html><head>Green
<!DOCTYPE html><html><head>Red

What I want is

<!DOCTYPE html><html><head>
Blue
Green
Red

So, then I can keep writing more information to the file via the POST form WITHOUT REPLACING THE FIRST LINE.

<!DOCTYPE html><html><head>

I know this is gonna be kind of a bad question, what what exactly can I do to achieve this?

Why don't you check if file is already with some data in it, for eg.:

$data_to_write = file_get_contents('testz.php'); 
$header_data = '<!DOCTYPE html><html><head>';
$file_data .= (empty($data_to_write))?$header_data.$data_to_write:$data_to_write;
file_put_contents('testz.php', $file_data);

You can only add to the end of a file. You can write in the middle of it but only writing over existing data. So in order to add lines of text always as the second line you should load the data, add the line in the right place, then write the file again.

But I think what you are doing is a very weird thing to do, and most likely avoidable. You are writing an HTML file which makes me think you are going to display it at some point, in which case you should separate data handling and presentation.

For example you can treat the file as a data file where you just append lines without a header, then when you want to serve the data as html you output a header first and then the lines in reverse order.

First of all, you should write the first line at the time you create the file, eg:

$first_line = "<!DOCTYPE html><html><head>" . PHP_EOL;
file_put_contents("somefile.txt", $first_line);

And when you want to insert new content you could load your file into an array with file() and insert the content at index 1 of the array (which is the second line in the file) by using array_splice() :

$lines = file("somefile.txt");
array_splice($lines, 1, 0, "new content"); 

Use implode() to join the array elements to a string and finally write to the file:

$file_content = implode(PHP_EOL, $lines);
file_put_contents("somefile.txt", $file_content);

Note that this approach will lead to memory issues when dealing with large files.

I ended up doing it this way,

<?php
$f=fopen('WriteFile.php', 'r+');
$W='<!DOCTYPE html>'.file_get_contents('WriteFile.php');
$S='New Data';
fwrite($f, "$W");
fgets($f);
fseek($f,15);
fwrite($f, "$S");
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM