Jump to content

Php Script - Write To Beginning Of Text File


Recommended Posts

Hello,

 

I'm VERY new to PHP, and I am trying to make a script that is supposed to run on my website(ftp) via Cron Jobs. The script should add a line of text to an existing text file on the website.

 

<?php
$fp_source = file_get_contents('file.txt');
$fp_dest = fopen('file_temp.txt', 'w'); // Makes a temporary file
$stringData = "\"model\";\"quantity\"\n";
fwrite($fp_dest, $stringData);
fwrite($fp_dest, $fp_source)
}
fclose($fp_source);
fclose($fp_dest);
unlink('dcs_prisliste.txt');
rename('dcsprisliste_temp.txt','dcs_prisliste.txt');
?>

 

The problem is that when this script runs the file-size of the file goes from 1.7 MB to 180 MB! Why is that?

 

Thanks,

Faxe3

Hi again,

 

Thanks for your answers.

 

 

That lone } bothers me. Did you post your exact script? The whole thing?

I think that was a typo :)

 

What requinix said.

 

Also, you should really be using file_put_contents () instead of fopen (), fwrite () and flcose ().

I've tried using file_put_contents istead:

<?php
$fp_source = file_get_contents('/public_html/file.txt');
$stringData = "\"model\";\"quantity\"\n";
file_put_contents("/public_html/file.txt", "");
file_put_contents("/public_html/file.txt", $stringData);
file_put_contents("/public_html/file.txt", $fp_source, FILE_APPEND);
?>

 

But it doesn't seam to work.

 

Thanks,

Faxe3

Edited by Faxe3

No need to call file_put_contents () multiple times. Prepare the data you want to save into the file, in the manner which you want it saved, and then write it. You should also check for errors, as mentioned in the PHP manual.

Without any error messages, or a better explanation of how it "doesn't seem to work", I'm afraid we cannot help you.

Hi,

 

I've tried making another script:

<?php
$file = 'file.txt'; 
$appendBefore = '\"model\";\"quantity\"\n';
$temp = file_get_contents($file);
$content = $appendBefore.$temp;
file_put_contents($file, $content);
?>

 

It doesn't write any error message, but the text is just not written to the file. Nothing happens.

 

Thanks,

What you need to do is to verify that the variables contain what you expect them to contain, and then check the return values from the function (file_put_contents () in this case) to ensure the call was successful. You haven't done so here, thus you have no indication of whether or not things are working.

What you need to do is to verify that the variables contain what you expect them to contain, and then check the return values from the function (file_put_contents () in this case) to ensure the call was successful. You haven't done so here, thus you have no indication of whether or not things are working.

Could you maybe help me do this? Because as I said I'm very new to PHP and I really don't know how I should do what you wrote.

 

Thanks,

Edited by Faxe3
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.