我有一个案例:
我在localhost(使用PHP)中使用cURL从共享主机下载zip文件。我有个问题。我还想在下载处理时停止下载。这可以停止下载吗?
因为,当我下载300MB的文件大小时,在进程的中间我重新加载浏览器并使用相同的PHP脚本再次访问,因此在Curl下载过程中有2个。
是否有停止下载的脚本(停止cURL进程下载)?
我的代码:
$path_download = "http://example.com/download/app.zip";
$target_download_file = "app.zip";
file_put_contents('progress.txt','');
$targetFile = fopen($target_download_file,'w');
$ch = curl_init($path_download);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_NOPROGRESS,false);
curl_setopt($ch,CURLOPT_PROGRESSFUNCTION,'progressCallback');
curl_setopt($ch,CURLOPT_BUFFERSIZE,128);
curl_setopt($ch,CURLOPT_FILE,$targetFile);
curl_exec($ch);
curl_close($ch);
fclose($targetFile);
function progressCallback( $download_size, $downloaded_size, $upload_size, $uploaded_size ){
static $previousProgress = 0;
if($download_size == 0)
$progress = 0;
else
$progress = round($downloaded_size * 100 / $download_size);
if ($progress > $previousProgress){
$previousProgress = $progress;
$fp = fopen('progress.txt','w');
fputs($fp,"$progress");
fclose( $fp );
}
}