大型CSV下载脚本超时

时间:2016-06-06 12:20:29

标签: php csv

我有一个脚本,可以上传CSV文件的内容并下载到本地目录的链接,我需要上传到它的CSV文件大约4056行和4056 FTP下载,脚本工作正常但是我使用它时,网络服务器超时。

甚至试过set_time_limit(0);

有没有办法可以对脚本进行会话并循环流程,这样就可以在没有中断的情况下完成几个小时的工作。

<?php  
/**
* Please change your upload directory according to your needs. Make sure you include the trailing slash!
*
* Windows    C:\tmp\
* Linux      /tmp/
*/
$uploaddir = '/tmp/';

if(isset($_FILES['userfile']['name'])){

    // Read uploaded file
    $lines  = file($_FILES['userfile']['tmp_name']);
    echo "Reading file ... <br/>";
    $linecount = 0;
    foreach($lines  as $line ){
        echo ++$linecount . ". FTP Url is : " .$line . "<br/>";
        echo "   Downloading " . $line . "<br/>";
        $parsed_url_values = parse_url($line);
        //TODO perhaps do a validation of the ftp url??

        if($parsed_url_values['scheme'] == 'ftp'){
                // set up basic connection
                $conn_id = ftp_connect($parsed_url_values['host']);

                // login with username and password
                $login_result = ftp_login($conn_id, $parsed_url_values['user'], $parsed_url_values['pass']);
                ftp_pasv($conn_id, true);
                $path = rtrim($parsed_url_values['path'], '_');
                $filename = basename($path);

                if (ftp_get($conn_id, $uploaddir . $filename, $path , FTP_BINARY)) {
                        echo "   Successfully downloaded the file " .$line . "<br/>";
                } else {
                        echo "   Could not save the file to " . $line . ". Please verify the url is correct and the file exists.<br/>";
                }
        } else {
                echo "   Sorry. This script was made for FTP downloads only.";
        }
    }
}
?>
<form enctype="multipart/form-data" action="" method="post">
  <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
  Select the file to upload : <input name="userfile" type="file" />
  <input type="submit" value="Upload" />
</form>

2 个答案:

答案 0 :(得分:0)

您需要使用max_execution_time来增加脚本的执行时间。您可以使用此示例,将其放在脚本之上:

<?php
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
.. The rest of your script

希望这会对你有帮助!

答案 1 :(得分:0)

在我的练习中,如果我无法通过cron或php cli加载数据,我经常会逐步加载。

类似的东西(未经测试):

<?php
$newFileName = '/path/to/file.csv';
$line = isset($_REQUEST['csvline']) ? (int) $_REQUEST['csvline'] : 0;
$handle = fopen($newFileName, "r");

//10 seconds for each step
$stepTime = 10;
$startTime = time();
$lineCounter = 0;
while (($fileop = fgetcsv($handle)) !== false)
{
    //already loaded lines
    $lineCounter++;
    if ($lineCounter <= $line) continue;
    //here goes your script logic
    //stops when time goes out
    if (time() - $startTime >= $stepTime) break;
}
//next you need to query this script again 
//using js maybe (window.location or ajax call)
//so you can see loading progress or any other useful information
//like
if (!feof($handle)) {
    echo "<script>window.location = '/your.php?csvline={$lineCounter}'<script>";
}
//you even can start loading from last line before script fails

但我确信使用cron是解决问题的最佳方案。