Hallo Zusammen
habe ein Script mit dem ich PHP Download Links erzeuge
ich weis das readfile() immer probleme mit großen files hat
hab schon mehrere versionen versucht, alle brechen immer irgendwo mitten drin ca. ab
ich bin am verzweifeln
brauche dringend eine lösung mit der ich dateien 200mb - 1GB ca. downloaden kann
Danke
Version 1:
Version 2:
habe ein Script mit dem ich PHP Download Links erzeuge
ich weis das readfile() immer probleme mit großen files hat
hab schon mehrere versionen versucht, alle brechen immer irgendwo mitten drin ca. ab
ich bin am verzweifeln
brauche dringend eine lösung mit der ich dateien 200mb - 1GB ca. downloaden kann
Danke
Version 1:
PHP-Code:
public static function processDownload($filePath, $filename)
{
jimport('joomla.filesystem.file');
$fsize = @filesize($filePath);
$mod_date = date('r', filemtime($filePath));
$cont_dis = 'attachment';
$ext = JFile::getExt($filename);
$mime = DMSHelper::getMimeType($ext);
// required for IE, otherwise Content-disposition is ignored
if (ini_get('zlib.output_compression'))
{
ini_set('zlib.output_compression', 'Off');
}
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition:' . $cont_dis . ';' . ' filename="' . JFile::getName($filename) . '";' . ' modification-date="' . $mod_date . '";' . ' size=' . $fsize . ';'); //RFC2183
header("Content-Type: " . $mime); // MIME type
header("Content-Length: " . $fsize);
if (!ini_get('safe_mode'))
{ // set_time_limit doesn't work in safe mode
@set_time_limit(0);
}
DMSHelper::readfile_chunked($filePath);
}
public static function readfile_chunked($filename, $retbytes = true)
{
$chunksize = 1 * (1024 * 1024); // how many bytes per chunk
$buffer = '';
$cnt = 0;
$handle = fopen($filename, 'rb');
if ($handle === false)
{
return false;
}
while (!feof($handle))
{
$buffer = fread($handle, $chunksize);
echo $buffer;
@ob_flush();
flush();
if ($retbytes)
{
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status)
{
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
PHP-Code:
public static function processDownload($filePath, $filename)
{
set_time_limit(0);
ini_set('memory_limit', '512M');
jimport('joomla.filesystem.file');
if(!empty($filePath))
{
$fileName = JFile::getName($filename);
$fileExtnesion = JFile::getExt($filename);
$contentType = DMSHelper::getMimeType($fileExtnesion);
if(file_exists($filePath))
{
$size = @filesize($filePath);
$offset = 0;
$length = $size;
//HEADERS FOR PARTIAL DOWNLOAD FACILITY BEGINS
if(isset($_SERVER['HTTP_RANGE']))
{
preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
$offset = intval($matches[1]);
$length = intval($matches[2]) - $offset;
$fhandle = fopen($filePath, 'r');
fseek($fhandle, $offset); // seek to the requested offset, this is 0 if it's not a partial content request
$data = fread($fhandle, $length);
fclose($fhandle);
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $size);
}//HEADERS FOR PARTIAL DOWNLOAD FACILITY BEGINS
//USUAL HEADERS FOR DOWNLOAD
header("Content-Disposition: attachment;filename=".$fileName);
header('Content-Type: '.$contentType);
header("Accept-Ranges: bytes");
header("Pragma: public");
header("Expires: -1");
header("Cache-Control: no-cache");
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
header("Content-Length: ".@filesize($filePath));
$chunksize = 10 * (1024 * 1024); //10MB (highest possible fread length)
if ($size > $chunksize)
{
$handle = fopen($filePath, 'rb');
$buffer = '';
while (!feof($handle) && (connection_status() === CONNECTION_NORMAL))
{
$buffer = fread($handle, $chunksize);
print $buffer;
ob_flush();
flush();
}
if(connection_status() !== CONNECTION_NORMAL)
{
echo "Connection aborted";
}
fclose($handle);
}
else
{
ob_clean();
flush();
readfile($filePath);
}
}
else
{
echo 'File does not exist!';
}
}
else
{
echo 'There is no file to download!';
}
}
Kommentar