Search code examples
phpcronfwrite

php fwrite issue when accessing the file


Hi i have a issue with my script.

I'm writing file contents using below code..and saving a file in my directory which users can access

$File = fopen('help/gossipcom.html','w'); 
fwrite($File, $header1 . $title2 . $footer1);  
fclose($File);

This is working perfectly..

But i'm running this using cron job

if a user access the file at the same time when the cron job runs, it will show a blank page..How can i avoid this issue?.

Thank you!


Solution

  • I think you only need to write the file and you are not intending for appending it every time the cron runs.

    What , i feel , you can do in this situation is you can create a temp file every time your cron runs with the name say

    TEMP FILE - help/gossipcomtemp.html
    

    Write your content in this file and after fclose() it, rename/mv the file with the actual name that is

    MAIN FILE - help/gossipcom.html
    

    This will , i think , ensure that your main file gets accessible when cron is running and this will not be locked both for writing and reading.