I created a simple file to download files.
I have the following code:
function DownloadFile($file) { // $file = include path
if(file_exists($file)) {
header('HTTP/1.0 200 OK', true, 200);
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile($file);
exit;
}
}
The problem is if I try to download files over 40-50MB the file is downloaded blank(0KB) but if I try below 40MB it is working perfectly.
What is the problem?
Like explained in this article https://www.sitepoint.com/community/t/php-file-size-download-limit/6541 you need to set
memory_limit = 128M
post_max_size = 300M
in php.ini file
The solution:
function DownloadFile($file) { // $file = include path
if(file_exists($file)) {
header('HTTP/1.0 200 OK', true, 200);
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
ob_end_flush();
if ($fd = fopen ($file, "r")) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
while(!feof($fd)) {
echo fread($fd, 4096);
flush();
}
}
}
}
Related
I want to download APK which is placed in my Windows server. The file exists. PHP script is given below
<?php
$file = '/45.34.14.285/n1/n.apk'; //not public folder
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
else
{
echo 'No File Found';
}
?>
But it returns
'No File Found'
When I try to download a file, it downloads but without content, i.e., its size is 0 bytes.
I am using this code, what I am doing wrong?
$fullPath = "/home/yakar/www/files/gpx/".$file;
if (file_exists($fullPath)) {
// setting headers
header('Content-Description: File Transfer');
header('Content-Type: '.$type);
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
} else {
die('File does not exist');
}
I'm trying to use PHP to serve up a file download. I can get the file to begin downloading, but it has only ever been able to download about 280MB of a 1GB file. I'm on a shared host so this could be the issue, if it's not an issue with my code:
function downloadFile($file){
if (file_exists($file)) {
if(is_dir($file)){return false;}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: '.sprintf("%u", filesize($file)));
ob_clean();
$handle = fopen($file, "rb");
$chunksize=(sprintf("%u", (filesize($file))/1024));
set_time_limit(0);
while (!feof($handle)) {
echo fgets($handle, $chunksize);
flush();
}
fclose($handle);
die;
}else{ echo "file not found";}
return;
}
I must download an apk file from my server, but i have an incomplete download.
This is the code:
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename="Zappapp1.0.apk"');
readfile('Zappapp1.0.apk');
EDIT
I try this but it not work..
$file = 'zappapp.altervista.org/apk/Zappapp1.0.apk';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
Sorry for my english but i'm italian ..
You Can Try This Maybe Its Help you.............
$file = '/home/bla-bla/domains/bla-bla.com/file/file.apk'; //not public folder
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
I have a problem with download script
when I download any file it is save with 0kb.
Any ideas? Here is my PHP script
<?php
$file = $_GET['fname'];
$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
ob_clean();
flush();
readfile($fullpath);
?>
Please add content-length header as well .
Please try this:-
<?php
$file = $_GET['fname'];
$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
$headers = get_headers($fullpath, 1);// use file absolute path here
$fsize = $headers['Content-Length'];
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header("Content-Length: " . $fsize);
ob_clean();
flush();
readfile($fullpath);
?>
Try below code hope it will help you.
I have tested this in my localhost.
$file = $_GET['fname'];
$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($fullpath));
ob_clean();
flush();
readfile($fullpath);