简体   繁体   中英

PHP Connection Timeout Issue

In one of my application, users can upload CSV file (| separated fields), after uploading I am storing all the content of file in temporary table (I truncate this table every time for new upload so that it contains the current file data). After that I am iterating over each and every row of that table, and performs some database operation as per the business logic.

The following code will illustrate this:

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*/                          
}
}

The problem is when I execute aboe script on server it is giving server timeout error. But when I test above script on my localhost, is is working fine.

Also as mentioned in the code, if I use echo statements for debugging, then it is working fine, and when I remove that it starts giving connection timeout problem.

I have tried set_time_limit(300) , set_time_limit(0) , but none of them seems to work.

Any idea, how can I resolve the above problem.

-- Many thanks for your time.

Edit:

I have checked that, files are uploading on the server.

set_time_limit

change to

ini_set("max_execution_time",300);

When max_execution_time is not set in php.ini set_time_limit valid.

I have resolved the issue using flush , to send intermediate output to the browser, while the query is executing in the background.

This is how I modified the code:

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

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

Thanks to the contributors over this link PHP: Possible to trickle-output to browser while waiting for database query to execute? , from where I got inspiration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM