ساخت لینک دانلود غیرمستقیم با قابلیت توقف و ادامه دانلود در php
با سلام
برای اولین نوشته میرم سراغ یه چیزی که خودمم توش خیلی گیر بودم و کلی گشتم تا مشکلش رو حل کنم.
برای دانلود غیرمستقیم با php اکثر سایت ها و وبلاگ ها میگفتن از کد زیر استفاده کنیم
header(“Pragma: public”);
header(“Expires: 0”);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
header(“Cache-Control: public”);
header(“Content-Description: File Transfer”);
header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=\””.basename($file).”\””);
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: “.filesize($file));
@readfile($file);
flush();
ولی خب کد بالا یه مشکلاتی داره ، اول از همه واسه فایل های سنگین خوب نیس و به سرور خیلی فشار میاره ، قابلیت ادامه دانلود نداره و سایت موقع دانلود از کار می افته تا دانلود تموم بشه .
واسه این که یه دانلود با لینک مستقیم داشته باشید بهتره از تابع زیر استفاده کنید
public function resumableDownload($file,$filename,$file_size,$content_type,$disposition = ‘inline’) {
@set_time_limit(0);
session_write_close();
// turn off compression on the server
@ini_set(‘zlib.output_compression’, ‘Off’);
$time = filemtime($file);
$etag = md5($file.$time);
$fileopen = fopen($file, “rb”);
// Download speed in KB/s
$chunk = 8*1024;
// Initialize the range of bytes to be transferred
$start = 0;
$end = $file_size-1;
// Check HTTP_RANGE variable
if (isset($_SERVER)
&& preg_match(‘/^bytes=(\d+)-(\d*)/’, $_SERVER, $arr)
) {
$start = $arr;
if ($arr) {
$end = $arr;
}
}
// Check if starting and ending byte is valid
if ($start > $end || $start > $file_size-1) {
header(“HTTP/1.1 416 Requested Range Not Satisfiable”);
header(“Content-Length: 0”);
return false;
} else {
// For the first time download
if ($start == 0 && $end == $file_size) {
// Send HTTP OK header
header(“HTTP/1.1 200 OK”);
} else {
// For resume download
header(“HTTP/1.1 206 Partial Content”);
header(“Content-Range: bytes “.$start.”-”.$end.”/”.$file_size);
}
// Bytes left
$left = $end-$start+1;
//set last-modified header
header(‘Last-Modified: ‘.date(‘D, d M Y H:i:s \G\M\T’, $time));
//set etag-header
header(‘ETag: “’.$etag.’”’);
// Send the other headers
header(“Accept-Ranges: bytes”);
header(“Content-Length: “.$left);
header(“Content-Type: $content_type”);
header(“Content-Disposition: $disposition; filename=\””.$filename.”\””);
header(“Content-Transfer-Encoding: binary”);
header(“Expires: -1”);
// Read file from the given starting bytes
fseek($fileopen, $start);
while (!feof($fileopen)) {
echo fread($fileopen, $chunk);
@ob_flush();
flush();
$left-=$chunk;
// // Delay for 10 microsecond
// usleep(10);
}
}
fclose($fileopen);
return true;
}
و اینجوری هم فراخونی میشه
incloud(rd.class.php);
$rd = new rd.class;
$rd->resumableDownload(‘File url’,’File name’,’file size’,’file type’);