PHP:写入文件时缺少记录 [英] PHP: Missing records when writing to file

查看:108
本文介绍了PHP:写入文件时缺少记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次出现消息时,我的电信供应商都会向我发送报告.我编写了一个非常简单的PHP脚本,该脚本通过HTTP GET接收值.我使用fwrite将查询参数写入CSV文件.文件名是report.csv,当前日期为前缀.

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.

这是代码:

<?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.

推荐答案

对此脚本我有几点建议.

I have a couple of suggestions for this script.

要解决Andrew Rhyne的建议,请将从每个$ GET变量读取的代码更改为:

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.

我怀疑您的问题还有其他问题.听起来您对要保存的每条记录都有单独的请求.也许其中一些请求正在关闭,并且使彼此的打开和写入文件的能力混乱.要检查这种情况是否发生,您可以尝试使用以下代码检查是否正确打开了文件. (请注意,您第一次在脚本中使用'fopen'并没有任何作用,因为您在第二次使用'fopen'时会覆盖$ handle,这也会打开错误的文件...)

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;
}

这将花费25秒的时间,尝试每半秒打开一次文件,并且每次仍然无法打开要写入的文件时,仍会将您的记录输出到唯一文件中.然后,您可以像以前一样安全地使用fwrite()和fclose()$ handle.

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.

这篇关于PHP:写入文件时缺少记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆