Faxe3 Posted September 24, 2012 Share Posted September 24, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268732-php-script-write-to-beginning-of-text-file/ Share on other sites More sharing options...
requinix Posted September 24, 2012 Share Posted September 24, 2012 That lone } bothers me. Did you post your exact script? The whole thing? Quote Link to comment https://forums.phpfreaks.com/topic/268732-php-script-write-to-beginning-of-text-file/#findComment-1380514 Share on other sites More sharing options...
Christian F. Posted September 24, 2012 Share Posted September 24, 2012 What requinix said. Also, you should really be using file_put_contents () instead of fopen (), fwrite () and flcose (). Quote Link to comment https://forums.phpfreaks.com/topic/268732-php-script-write-to-beginning-of-text-file/#findComment-1380519 Share on other sites More sharing options...
Faxe3 Posted September 24, 2012 Author Share Posted September 24, 2012 (edited) 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 September 24, 2012 by Faxe3 Quote Link to comment https://forums.phpfreaks.com/topic/268732-php-script-write-to-beginning-of-text-file/#findComment-1380537 Share on other sites More sharing options...
Christian F. Posted September 24, 2012 Share Posted September 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268732-php-script-write-to-beginning-of-text-file/#findComment-1380553 Share on other sites More sharing options...
Faxe3 Posted September 24, 2012 Author Share Posted September 24, 2012 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, Quote Link to comment https://forums.phpfreaks.com/topic/268732-php-script-write-to-beginning-of-text-file/#findComment-1380599 Share on other sites More sharing options...
Christian F. Posted September 24, 2012 Share Posted September 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268732-php-script-write-to-beginning-of-text-file/#findComment-1380668 Share on other sites More sharing options...
Faxe3 Posted September 25, 2012 Author Share Posted September 25, 2012 (edited) 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 September 25, 2012 by Faxe3 Quote Link to comment https://forums.phpfreaks.com/topic/268732-php-script-write-to-beginning-of-text-file/#findComment-1380761 Share on other sites More sharing options...
Christian F. Posted September 25, 2012 Share Posted September 25, 2012 Jessica's signature (random post of hers) contains links to multiple resources which explain how to do this, and more. I strongly recommend that you read through them, and trying out what's detailed there. Quote Link to comment https://forums.phpfreaks.com/topic/268732-php-script-write-to-beginning-of-text-file/#findComment-1380801 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.