keystrike Posted December 19, 2009 Share Posted December 19, 2009 I am having a problem with PHP writing files. I have a set of PHP scripts that create files on the site, basically a rudimentary content management system that automatically creates each week's edition onsite from the database. However, there seems to be an issue with PHP writing files to the server - it works perfectly well the first time I try, but any subsequent attempts to write the same file fail unless I wait for 5 minutes or so. It is as if the file, once written, if cached for a few minutes. If I wait a few minutes I am able to write to the file again fine, but then have to wait another few minutes in order to access the file. Indecently, the same behavior is present on my Debian box at home with a default Apache / PHP setup, but not on any of my Windows boxes, so I guess this is a generic Linux / Debian issue. I've attached an example of the code, but it affects quite a few scripts I have. I've tried it using the fopen(), fwrite(), fclose() combo, and also with the simpler file_put_contents() function, both with the same result. In the attached script you can run it once, then change the contents of the $homepage variable (via changes to the SQL database it draws its data from) and then run the scrip again, but on the second run it wont update the target file unless I wait for a few minutes. <?php /* GET GLOBAL CONFIG FILE AND FUNCTIONS */ require_once '../storyAdmin/functions.php'; /* DATA PROCESSING SECTION */ // GET ISSUE ID if (isset($_GET['issue']) && is_numeric($_GET['issue'])) { $issueId = $_GET['issue']; } if (!isset($issueId)) { if (DEBUG) echo "<br>edition.php :: data processing :: issueId not set ;; exiting silently <br>"; exit; } require '../storyAdmin/storyCreationFunctions.php'; // Homepage will be returned as a string, which is basically a full HTML file... $homepage = makeHomepage($issueId); if (file_exists('../index_temp.php')) unlink ('../index_temp.php'); if (!file_put_contents('../index_temp.php', $homepage)) { echo "<h1>Couldn't write file</h1>"; } $link = dirname($_SERVER['REQUEST_URI']).'/../index_temp.php'; //echo $_SERVER['REQUEST_URI']."<br />"; //echo $link; header("Location: $link"); exit; ?> Thanks! Link to comment https://forums.phpfreaks.com/topic/185726-issue-with-php-writing-files-to-the-server/ Share on other sites More sharing options...
teamatomic Posted December 20, 2009 Share Posted December 20, 2009 I think you are creating what is called a race condition by unlinking and writing to the same file. Unlink is not finished and you try to start writing to the file that is being unlinked. Make sense? The proper way to do this is to make a temp with the new index contents of 'file_tmp.txt' copy file_tmp.txt to index_temp.php unlink file_tmp.txt Link to comment https://forums.phpfreaks.com/topic/185726-issue-with-php-writing-files-to-the-server/#findComment-980682 Share on other sites More sharing options...
gevensen Posted December 20, 2009 Share Posted December 20, 2009 have you tried adding ob_start(); at the very 1st line and ob_end_flush(); at the very last line? Link to comment https://forums.phpfreaks.com/topic/185726-issue-with-php-writing-files-to-the-server/#findComment-980710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.