简体   繁体   English

如何使用PHP中的CURLOPT_FILE判断curl下载是否完成

[英]How to tell if curl download is complete using CURLOPT_FILE in PHP

I have a function that downloads a specific zip file from a remote server to my local Windows server. 我有一个功能,可以将特定的zip文件从远程服务器下载到本地Windows服务器。 The file ranges in size from 1-10 mb and I want my script to wait until it's complete. 该文件的大小在1到10 mb之间,我希望脚本等待完成。 How can I tell with certainty that it's done? 我如何确定地确定完成了? The actual downloading works fine. 实际的下载工作正常。

The function is: 该函数是:

function get_download($url, $path) 
{ 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL,$url); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie2.txt'); 
    curl_setopt($curl, CURLOPT_FILE, $path);
    $results = curl_exec($curl); 
    return $results; 
}

I thought I would get the size of the remote file first then compare to the local file size. 我以为我会先获取远程文件的大小,然后再与本地文件大小进行比较。 However, this doesn't seem to work. 但是,这似乎不起作用。 Not sure if its a function of the file being a .zip file but it always returns -1 or -11. 不确定其功能是否为.zip文件,但始终返回-1或-11。 I tried this function: 我试过这个功能:

function remotefileSize($url) 
{
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_NOBODY, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
    curl_exec($curl);
    $filesize = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    curl_close($curl);
    return $filesize;
}

Did you see anything on your browser that shows Unknown time remaining or Unknown bytes remaining while downloading a file? 您在浏览器上是否看到任何显示下载文件Unknown time remaining Unknown bytes remainingUnknown bytes remaining的信息? It is because the browser couldn't retrieve the size and failed to calculate remaining time or size. 这是因为浏览器无法检索大小,并且无法计算剩余时间或大小。 This is what is happening with you. 这就是你正在发生的事情。 Your filesize retrieval code is correct. 您的文件大小检索代码正确。

Another reason could be: Sometime the server doesn't support the HEAD request(when you use CURLOPT_NOBODY ). 另一个原因可能是:有时服务器不支持HEAD请求(当您使用CURLOPT_NOBODY )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM