PHP: Missing records when writing to file

人盡茶涼 提交于 2019-12-24 05:43:55

问题


My telecom vendor is sending me a report each time a message goes out. I have written a very simple PHP script that receive values via HTTP GET. Using fwrite I write the query parameter to a CSV file.The filename is report.csv with the current date as a prefix.

Here is the code :

<?php
error_reporting(E_ALL ^ E_NOTICE);
date_default_timezone_set('America/New_York'); 
//setting a the CSV File
$fileDate = date("m-d-Y") ;
$filename = $fileDate."_Report.csv";

$directory = "./csv_archive/";

//Creating handle
$handle = fopen($filename, "a");

//These are the main data field
$item1 = $_GET['item1'];
$item2 = $_GET['item2'];
$item3 = $_GET['item3'];

$mydate = date("Y-m-d H:i:s") ;
$pass = $_GET['pass'];

//testing the pass
if (isset($_GET['pass']) AND $_GET['pass'] == "password")
    {
    echo 'Login successful';
    // just making sure the function could write to it
    if (!$handle = fopen($directory.$filename, 'a')){
         echo "Cannot open file ($filename)";
         exit;
    }

    //writing the data I receive through query string
    if (fwrite($handle, "$item1,$item2,$item3,$mydate \n") === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    fclose($handle); 
    }
else{
    echo 'Login Failure please add the right pass to URL';   
    }   
?>

The script does what I want, but the only problem is inconsistency, meaning that a good portion of the records are missing (about half the report). When I log to my account I can get the complete report.

I have no clue of what I need to do to fix this, please advice.


回答1:


I have a couple of suggestions for this script.

To address Andrew Rhyne's suggestion, change your code that reads from each $GET variable to:

$item1 = (isset($_GET['item1']) && $_GET['item1']) ? $_GET['item1'] : 'empty';

This will tell you if all your fields are being populated.

I suspect you problem is something else. It sounds like you are getting a seperate request for each record that you want to save. Perhaps some of these requests are happening to close together and are messing up each other's ability to open and write to the file. To check if this is happening, you might try using the following code check if you opened the file correctly. (Note that your first use of 'fopen' in your script does nothing, because you are overwriting $handle with your second use of 'fopen', it is also opening the wrong file...)

if (!$handle = fopen($directory.$filename, 'a')){
     $handle = fopen($directory.date("Y-m-d H:i:s:u").'_Record_Error.txt', 'a');
     exit;
}

This will make sure that you don't ever lose data because of concurrent write attempts. If you find that this is indeed you issue, you can delay subsequent write attempts until the file is not busy.

$tries = 0;
while ($tries < 50 && !$handle = fopen($directory.$filename, 'a')){
     sleep(.5);//wait half a second
     $tries++;
}
if($handle){
    flock($handle);//lock the file to prevent other requests from opening the file until you are done.
} else {
    $handle = fopen($directory.date("Y-m-d H:i:s:u").'_Record_Error.txt', 'a');//the 'u' is for milliseconds
    exit;
}

This will spend 25 seconds, trying to open the file once every half second and will still output your record to a unique file every time you are still unable to open the file to write to. You can then safely fwrite() and fclose() $handle as you were.



来源:https://stackoverflow.com/questions/14592677/php-missing-records-when-writing-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!