【问题标题】:Downloading files from server using PHP使用 PHP 从服务器下载文件
【发布时间】:2012-01-24 21:11:14
【问题描述】:

我有一个 Web 服务器,其中一个文件夹中有 1000 个文件,名称如下:

1.txt
2.txt
3.txt
.
.
.
1000.txt

注意:文件也可能是其他格式。比如JPEG、PNG等。 我想使用 PHP 下载它们(我可以访问该文件夹)。

我正在尝试使用以下算法:

for (i=1 to 1000)
{
     $link = "http://xyz.com/pqr/".$i.".txt";
     fopen($link);
     /*download -- how to?*/
}

这个方法正确吗?如何复制/下载到我的本地机器?

【问题讨论】:

    标签: php apache download


    【解决方案1】:

    【讨论】:

    【解决方案2】:

    你可以使用cURL:

    function get_data($link)
    {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $link);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      相关资源
      最近更新 更多