PHP连接超时问题

时间:2013-07-23 06:26:47

标签: php file-upload connection

在我的一个应用程序中,用户可以上传CSV文件(|分隔字段),上传后我将文件的所有内容存储在临时表中(我每次都会截断此表进行新上传,以便它包含当前文件数据)。之后,我迭代该表的每一行,并根据业务逻辑执行一些数据库操作。

以下代码将说明这一点:

if(isset($_POST['btn_uploadcsv']))    
{         
$filename = $_FILES["csvupload"]["name"];
$uploads_dir = 'csvs'; //csv files...
$tmp_name = $_FILES["csvupload"]["tmp_name"];
$name =  time();
move_uploaded_file($tmp_name, "$uploads_dir/$name");

$csvpath = "$uploads_dir/$name";

$row = 0;
$emptysql = "TRUNCATE TABLE `temp`";
$connector->query($emptysql);

if (($handle = fopen($csvpath, "r")) !== FALSE) {
    $str_ins = "";
    while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
                    /*
                     *     Here I am getting the column values to be store in the 
                     *     the table, using INSERT command
                     */
                    unset($data);
    }
            fclose($handle);
}

    /*Here I am selecting above stored data using SELECT statement */

for($j=0;$j<count($allrecords);$j++)
{
        echo "In the loop"; 
         /*If I use echo statement for debugging it is working fine*/

        //set_time_limit(300);  
        /* I have tried this also but it is not working*/

        if(!empty($allrecords[$j]['catid']))
        {
         // Here is my business logic which mailny deals with 
         // conditional DB operation    
        }

        echo "Iteration done."; 
        /*If I use echo statement for debugging it is working fine*/                          
}
}

问题是当我在服务器上执行aboe脚本时,它会给出服务器超时错误。但是当我在我的localhost上测试上面的脚本时,工作正常。

同样如代码中所提到的,如果我使用echo语句进行调试,那么它工作正常,当我删除它时会开始给出连接超时问题。

我尝试了set_time_limit(300)set_time_limit(0),但似乎都没有。

任何想法,我该如何解决上述问题。

- 非常感谢你的时间。

修改

我已经检查过,文件正在服务器上传。

2 个答案:

答案 0 :(得分:0)

set_time_limit

更改为

ini_set("max_execution_time",300);

如果未在php.ini中设置max_execution_time,则set_time_limit有效。

答案 1 :(得分:0)

我已使用flush解决了该问题,将中间输出发送到浏览器,而查询正在后台执行。

这是我修改代码的方式:

for($j=0;$j<count($allrecords);$j++)
{

  /*At the end of each iteration, I have added the following code*/   
   echo " ";
   flush();                   
}

感谢贡献者通过这个链接PHP: Possible to trickle-output to browser while waiting for database query to execute?,从中我获得了灵感。

相关问题