【问题标题】:Large CSV download script timing out大型 CSV 下载脚本超时
【发布时间】:2016-06-06 12:20:29
【问题描述】:

我有一个脚本,可以上传 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>

【问题讨论】:

    标签: php csv


    【解决方案1】:

    您需要使用max_execution_time 来增加脚本的执行时间。你可以使用这个例子,把它放在你的脚本之上:

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

    希望对您有所帮助!

    【讨论】:

    • 让我试一试
    • 仍然收到 500 错误,开始认为我需要更改它并将其设置为 cron 作业,除非我能找到循环它的方法。我在虚拟机上运行 ubuntu 并让它运行,但这仍然是双重处理,而且似乎也很慢。
    • 不只是 500 错误脚本工作正常,我只是拆分 csv,它甚至不能在 1 次坐下做 100 行。我没有时间为我的零售网站获取这些图像文件,我的批发商只需要从 http 更改为 ftp 即可获取文件
    • 不,这失败了,通过 cron 运行它被拒绝了
    【解决方案2】:

    在我的实践中,如果我无法通过 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 是解决您问题的最佳方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      相关资源
      最近更新 更多