【问题标题】:Best method for automating bulk file download自动化批量文件下载的最佳方法
【发布时间】:2014-03-06 20:12:33
【问题描述】:

我正在尝试创建一个 cron 作业来下载存储在我们数据库中的队列中的图像文件。

我们使用的所有功能在我们的网络服务器上运行时都能正常工作,但是当我使用以下命令运行 cron 作业时:php index.php cron image_download 我收到 Segmentation Fault 错误。

调试cron作业显示数据传递给get_url_content函数时出现此错误,这里调用该函数:

foreach($urls as $url){

    $content = $this->get_url_content($url); 
}

功能在这里:

function get_url_content($url){
    $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_URL,$url);
    return curl_exec($ch);
}

有没有更好的方法来下载这些文件?不同的方法是否可能不会导致相同的分段错误错误?谢谢!

更新:我正在尝试的各种方法似乎不断导致问题。我看到 cron 作业返回“Segmentation Fault”或“Killed”错误。有人建议我为此考虑使用Iron.io,所以我要检查一下。如果有人对如何最好地管理这个有其他建议,我将不胜感激,谢谢。

【问题讨论】:

  • 另外请解释你的命令php index.php cron image_download,即php index.php arg1 arg2argv[0] 将是 index.phpargv[1] 将是 arg1... 等等。
  • 很酷,感谢到目前为止的答案......我的 localhost 配置有些问题,我无法在我的 localhost 上运行 cron 作业,所以我必须在实时服务器环境中测试这些。我还没有这样做,但明天会。
  • 是 cron 的问题没有解决,还是 php scipt php index.php cron image_download 本身不起作用? *您使用的所有功能在您的网络服务上运行时如何正常工作*`?
  • cron 不起作用,它返回“分段错误”。当我从网络浏览器运行该功能时,它可以工作。

标签: php curl cron segmentation-fault download


【解决方案1】:

您可以尝试这种方法,但在此之前,您是否提供了完整的 URL?

function get_content($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, false);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec($ch);
    curl_close($ch);
    return($result);
}

function save_content($text,$new_filename){
    $fp = fopen($new_filename, 'w');
    fwrite($fp, $text);
    fclose($fp);
}

// replace this with your array of urls from the database (make sure it is an array)
$urls = ['http://domain.com/path/to/file.zip', 'http://another.com/path/to/image.img'];

foreach($urls as $url){
    $new_filename = basename($url);
    $temp = get_content($url);
    save_content($temp,$new_filename);
}

这将通过其完整的 url 获取文件内容并将其保存到磁盘,从而被下载。


如果你不局限于 curl,你可以试试这样的:

$urls = ['http://domain.com/path/to/file.zip', 'http://another.com/path/to/image.img'];

foreach($urls as $url){
    $new_filename = basename($url);
    // or fopen can be file_get_contents: file_get_contents($url)
    file_put_contents($new_filename, fopen($url, 'r'));
}

甚至

foreach($urls as $url){
    $new_filename = basename($url);
    shell_exec("wget $url -O $new_filename");
}

【讨论】:

  • 好吧,看来我发现调用这些函数的方式存在结构性问题。这个答案有助于我到达那里,所以谢谢!
【解决方案2】:

使用 curl 选项CURLOPT_FILE 将文件直接从 curl 下载到文件中。对于这种情况,我已经从您现有的代码中注释掉了另外两个选项。这是您修改后的函数:

function get_url_content($url, $file){
    $fp = fopen ($file, 'w+');                   // open file handle

    $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FILE, $fp);          // output to file
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  // handle redirect
    // curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_exec($ch);
    fclose($fp);                                  // closing file handle
}

注意,我在函数中添加了第二个参数(文件名为$file)。因此,只需将您的 url 和文件路径(当然是绝对路径)传递给它。

如果您精通Shell scripting,您也可以使用 curl 的命令行选项来下载文件。比如这个命令会将图片下载到指定的文件中。

curl -s -L "http://img_url/" -o /var/path/image.jpeg

【讨论】:

    【解决方案3】:

    我花了很长时间寻找其他方法来进行这种多文件下载,然后才发现一个简单的解决方案是使用 zipArchive。这允许您创建和打开一个 zip 文件,向其中添加文件,然后关闭它。然后,您可以创建指向存档的 Web 链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 2023-03-13
      • 2017-06-22
      相关资源
      最近更新 更多