开发者

php Curl download tbz file?

开发者 https://www.devze.com 2023-04-12 07:23 出处:网络
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

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_B开发者_如何转开发INARYTRANSFER, 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?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号