php Curl 下载 tbz 文件?

发布于 2024-12-09 10:35:44 字数 949 浏览 0 评论 0原文

我检查了SO,也用谷歌搜索,但没有找到任何方法来解决我的问题。

我想下载一个扩展名为 tbz 的文件,但它给了我错误,因为

Warning: feof() expects parameter 1 to be resource, string given in D:\xampp\htdocs\test\testing.php on line **

我使用curl 从服务器下载该文件。

        set_time_limit(0);
        $ch = curl_init();
        $timeout = 0;
        curl_setopt ($ch, CURLOPT_URL, $webpage_url);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

        $handler = curl_exec($ch);

        $fp = fopen("adminArea/test/$file", "w+");
        $contents = '';
        while (!feof($handler)) {
          $contents .= fread($handler, 8192);
          fwrite($fp, $handler);
        }
        fclose($fp);

        curl_close($ch);  

我的文件以 MB 为单位,当我执行此操作时,我会得到更大的文件。如果我的文件大小为 16m,当我运行代码时,它需要很长时间,而当我手动停止它时,它的大小为 146MB。我做错了什么?

I checked SO and also googled but not find any way to resolve my issue.

i want to download a file with tbz extension but it is giving me error as

Warning: feof() expects parameter 1 to be resource, string given in D:\xampp\htdocs\test\testing.php on line **

I am using curl to download this file from the server.

        set_time_limit(0);
        $ch = curl_init();
        $timeout = 0;
        curl_setopt ($ch, CURLOPT_URL, $webpage_url);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

        $handler = curl_exec($ch);

        $fp = fopen("adminArea/test/$file", "w+");
        $contents = '';
        while (!feof($handler)) {
          $contents .= fread($handler, 8192);
          fwrite($fp, $handler);
        }
        fclose($fp);

        curl_close($ch);  

my files are in MB and when i execute this i get file of a larger size. if my file is 16m of size when i run the code it takes alot time and when i stopped it manually it was 146MB of size. what i am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-12-16 10:35:44

我不认为您尝试访问的文件类型对代码的工作方式有太大影响。

首先,当设置CURLOPT_RETURNTRANSFER时,curl_exec将返回数据。
其次,feof、fread 等需要一个文件句柄,而不是 cURL 句柄或返回的数据(这就是为什么您的错误消息显示您正在发送字符串)。

您的代码可能应该是这样的:

    set_time_limit(0);
    $ch = curl_init();
    $timeout = 0;
    curl_setopt ($ch, CURLOPT_URL, $webpage_url);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

    file_put_contents("adminArea/test/$file", curl_exec($ch));
    curl_close($ch);

尽管我会在提交任何内容之前亲自测试错误(除非这只是为了个人一次性使用)。

I don't believe the type of file you're trying to access has much impact on how the code works.

Firstly, when CURLOPT_RETURNTRANSFER is set, curl_exec will return the data.
Secondly, feof, fread etc require a file handle, not a cURL handle or the returned data (which is why your error message is showing that you're sending a string through).

Your code should probably be something like this instead:

    set_time_limit(0);
    $ch = curl_init();
    $timeout = 0;
    curl_setopt ($ch, CURLOPT_URL, $webpage_url);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

    file_put_contents("adminArea/test/$file", curl_exec($ch));
    curl_close($ch);

Although I would personally test for errors before committing anything (unless this is, say, just for personal once-off use).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文