php Curl 下载 tbz 文件?
我检查了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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为您尝试访问的文件类型对代码的工作方式有太大影响。
首先,当设置CURLOPT_RETURNTRANSFER时,curl_exec将返回数据。
其次,feof、fread 等需要一个文件句柄,而不是 cURL 句柄或返回的数据(这就是为什么您的错误消息显示您正在发送字符串)。
您的代码可能应该是这样的:
尽管我会在提交任何内容之前亲自测试错误(除非这只是为了个人一次性使用)。
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:
Although I would personally test for errors before committing anything (unless this is, say, just for personal once-off use).