我创建脚本以从某个文件服务器下载文件但不能正常工作。
function downloadPage2($url){
$ch = curl_init();
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 8096);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $buffer) {
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
header('Cache-Control: private', false);
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="mp3.mp3"');
header('Content-Transfer-Encoding: binary');
// header('Content-Length: ' . strlen($buffer)); // provide file size
header('Connection: close');
echo $buffer;
return strlen($buffer);
});
curl_exec ($ch);
curl_close($ch);
}
当从服务器输入$ url to file(mp3)时,它会下载此文件但不能正确下载文件。示例:服务器上的文件大小为4.5mb,我的脚本下载了它,但大小为6mb。音乐正在播放,但每秒中断。 你知道哪里有问题吗?
我尝试在标题中设置content-lenght,但每个函数都返回内容lengh = 0
答案 0 :(得分:0)
试试这个,它适用于我下载大文件
$(".draggable").draggable({
revert: true,
appendTo: "body",
helper: 'clone',
start: function(event, ui) {
$(this).fadeTo('fast', 0.5);
},
stop: function(event, ui) {
$(this).fadeTo(0, 1);
}
});
答案 1 :(得分:0)
此代码对我来说很好用,我正在使用php readfile
函数根据readfile
的文档从远程主机读取文件:
如果启用了fopen_wrappers,则可以将此功能用作URL的文件名。有关如何指定文件名的更多详细信息,请参见fopen()。请参阅支持的协议和包装器,以获取有关各种包装器具有的功能,使用说明以及它们可能提供的任何预定义变量的信息的链接。
header('Content-Type:'.$this->get_mime_type($url));
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=".$filename);
echo readfile($url);
function get_mime_type($filename) {
$idx = explode( '.', $filename );
$count_explode = count($idx);
$idx = strtolower($idx[$count_explode-1]);
$mimet = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'docx' => 'application/msword',
'xlsx' => 'application/vnd.ms-excel',
'pptx' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
if (isset( $mimet[$idx] )) {
return $mimet[$idx];
} else {
return 'application/octet-stream';
}
}