I want to create a download link, and I found an example online. I forget where I get it.
Here is the code:
<?php
// place this code inside a php file and call it f.e. "download.php"
//$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$path = "music/";
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
//case "pdf":
//header("Content-type: application/pdf"); // add here more headers for diff. extensions
//header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
//break;
case "mp3":
header("Content-type: audio/mpeg"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
case "wma":
header("Content-type: audio/wma"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
case "ogg":
header("Content-type: audio/ogg"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default:
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
fclose ($fd);
}
exit;
// example: place this kind of link into the document where the file download is offered:
// Download here
?>
The problem is when I click the download link, it works. But after few seconds, my whole computer freezes and I need to force a shutdown.
What is the cause of this problem? Is it my computer fault or the code? I am using WAMP Server 2.
Update :
I tried to use readfile() instead of fread(), however I still get the same problem. Below is my code :
<?php
$directory_load = simplexml_load_file('conf/configuration.xml');
$path = $_SERVER['SERVER_ROOT'].$directory_load -> directories;
echo $path;
if($_GET['download_file'] != NULL) {
$fullPath = $path.$_GET['download_file'];
if (file_exists($fullPath)) {
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');
header('Pragma: public');
header('Content-Length: ' . filesize($fullPath));
ob_clean();
flush();
readfile($fullPath);
exit;
}
else {
echo "<span style=\"font-size: 15px;\">The file you requested to download <span style=\"font-size: 30px; font-weight: bold; color: red; padding: 0px 20px;\">".$_GET['download_file']."</span> does not exists.</span>";
}
}
else {
header("location: index.php");
}
?>
UPDATE 2 :
I tested the code with Mozilla version 21.0, it works fine. But as I stated, my computer freeze when I click the download link, it is when I am using Coolnovo (ChromePlus) Version 2.0.8.33. Is it related to the browser?
Try clearing the buffer and flushing headers. See example below:
header("Content-type: text/csv");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="' . $download_info->original . '"');
ob_clean(); // discard any data in the output buffer (if possible)
flush(); // flush headers (if possible)
readfile($path . $download_info->renamed);
SureVerify - Email Verification
Related
I am working on existing products and i am trying to resolve problem with downloading xlsx file.
When i download file through file manager, file is allright. But when i download file through Yii app, the file is corrupted.
Here is my code
public function actionCallLog($hash, $filename)
{
var_dump(
$hash, $filename
);
ignore_user_abort(true);
set_time_limit(0);
$path = "/var/www/html/uploads/call_log/".$hash.'/';
$dl_file= basename($filename).'.xlsx';
if (!file_exists($path.$dl_file)) {
$dl_file= basename($filename).'.csv';
}
$fullPath = $path.$dl_file;
if ($fd = fopen ($fullPath, "r"))
{
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext)
{
case "xlsx":
header('Content-Description: File Transfer');
header("Content-type: application/xlsx");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "csv":
header('Content-Description: File Transfer');
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
// add more headers for other content types here
default;
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
while(!feof($fd))
{
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
}
Does anybody know where can be the problem?
Thanks
Try turning off all the header instructions (and also the "echo $buffer;" as that might be a lot on screen) and see if any warnings or errors is printed on screen when opening the url for the file download.
If the corrupted file has a size, your code might actually give a warning or generate other output which will become part of the file because it is also echo'ed. Obviously that extra plain text output by your code would turn your correct xlsx into a corrupt file. You might be able to get that text by opening your corrupted file in a text-editor as it would show in there in plain text (probably at the start).
try add the response format
\Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
and for xlsx the content type should be
'application/vnd.ms-excel'
I've got a php file downloader that I've been using to download a word document, now I need to allow it to download pdfs and it won't open the document stating that the pdf is corrupted.
I've looked up the content-type, ect and t appears to be correct for pdfs, however, I'm unsure of what else it could be. Am I missing some more headers that are necessary for downloading pdfs? I'm very new to this so I'm uncertain of all the possible problems.
my code:
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "docx":
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
<?php
header("Content-Type: application/pdf");
header("Content-Type: application/x-download");
header('Content-disposition: attachment; filename="test.pdf"');
header('Content-length: ' . filesize('test.pdf'));
readfile('test.pdf');
This works for me every time. You can try it.
I am trying to download a large file using the script below. The file downloads, but its named 'download' and the file extension is missing. How can I modify the code below so that the original file name and extension is preserved ? Also is there anyway to automatically detect the mime type and include that as well ?
Thanks a lot in advance.
$path = 'public/Uploads/Films/files/Crank2006.avi';
$size=filesize($path);
$fm=#fopen($path,'rb');
if(!$fm) {
// You can also redirect here
header ("HTTP/1.0 404 Not Found");
die();
}
$begin=0;
$end=$size;
if(isset($_SERVER['HTTP_RANGE'])) {
if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
$begin=intval($matches[0]);
if(!empty($matches[1])) {
$end=intval($matches[1]);
}
}
}
if($begin>0||$end<$size)
header('HTTP/1.0 206 Partial Content');
else
header('HTTP/1.0 200 OK');
header("Content-Type: video/avi");
header('Accept-Ranges: bytes');
header('Content-Length:'.($end-$begin));
header("Content-Disposition: inline;");
header("Content-Range: bytes $begin-$end/$size");
header("Content-Transfer-Encoding: binary\n");
header('Connection: close');
$cur=$begin;
fseek($fm,$begin,0);
while(!feof($fm)&&$cur<$end&&(connection_status()==0))
{ print fread($fm,min(1024*16,$end-$cur));
$cur+=1024*16;
usleep(1000);
}
die();
In Content Disposition header you need to specify file name
$file_url = 'what you want to set'
header("Content-disposition: attachment; filename=\"" . $file_url. "\"");
A good tutorial on forced download php here.
PHP Force Download.
For mime type see the following SO post
Detecting mine-type in PHP.
Just do this below the $path
$path = 'public/Uploads/Films/files/Crank2006.avi';
$filename = array_pop(explode('/',$path)); // Grabbing the filename ... it will be Crank2006.avi
and add the header with filename to your existing headers.
header("Content-disposition: filename=$filename");
EDIT:
Detecting MIME type...
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $filename);
Try Something Like Below
$file='test.pdf' //File to download with Large Size
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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('backup/'.$file);
return 1;
} else {
return 0;
}
I have created a file, using fopen('contacts','w').
now i want to prompt user to save this file where he want in his local machine(using php).
Any suggestion or sample code will be appreciated.
Thanks!!
Assuming your file - "contacts" is a physical file exists now in the server.
<?php
$file = 'contacts.csv';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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;
}
?>
Ref: http://php.net/manual/en/function.readfile.php
Download code
<?php
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// Download here
?>
I'm trying to force download files from my web server using PHP.
I'm not a pro in PHP but I just can't seem to get around the problem of files downloading in 0 bytes in size.
CODE:
$filename = "FILENAME...";
header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
set_time_limit(0);
readfile($file);
Can anybody help?
Thanks.
You're not checking that the file exists. Try using this:
$file = 'monkey.gif';
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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 "File does not exists";
}
And see what you get.
You should also note that this forces a download as an octet stream, a plain binary file. Some browsers will struggle to understand the exact type of the file. If, for example, you send a GIF with a header of Content-Type: application/octet-stream, then the browser may not treat it like a GIF image. You should add in specific checks to determine what the content type of the file is, and send an appropriate Content-Type header.
I use the following method in phunction and I haven't had any issues with it so far:
function Download($path, $speed = null)
{
if (is_file($path) === true)
{
$file = #fopen($path, 'rb');
$speed = (isset($speed) === true) ? round($speed * 1024) : 524288;
if (is_resource($file) === true)
{
set_time_limit(0);
ignore_user_abort(false);
while (ob_get_level() > 0)
{
ob_end_clean();
}
header('Expires: 0');
header('Pragma: public');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . sprintf('%u', filesize($path)));
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
header('Content-Transfer-Encoding: binary');
while (feof($file) !== true)
{
echo fread($file, $speed);
while (ob_get_level() > 0)
{
ob_end_flush();
}
flush();
sleep(1);
}
fclose($file);
}
exit();
}
return false;
}
You can try it simply by doing:
Download('/path/to/file.ext');
You need to specify the Content-Length header:
header("Content-Length: " . filesize($filename));
Also, you shouldn't send a Content-Transfer-Encoding header. Both of the HTTP/1.0 and HTTP/1.1 specs state that "HTTP does not use the Content-Transfer-Encoding (CTE) field of RFC 1521".
This problem as same as my website project. This code I've used:
<?php
$file = $_SERVER["DOCUMENT_ROOT"].'/.../.../'.$_GET['file'];
if(!file)
{
// File doesn't exist, output error
die('file not found');
}
else
{
//$file_extension = strtolower(substr(strrchr($file,"."),1));
$file_extension = end(explode(".", $file));
switch( $fileExtension)
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
nocache_headers();
// Set headers
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); // required for certain browsers
header("Content-Description: File Transfer");
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=".$file.";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile($file);
}
?>
I think the problem is on the server setting like PHP setting or cache setting, but I don't have any idea to do this my opinion.
i am doing this to download a PDF ...
$filename = 'Application.pdf';
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=$filename");
echo $pdf;
i think you are missing the last row, where you actually send the file contents of whatever you have in $file.
Pablo
The file opened ok for me when I changed the directory to the file location.
$reportLocation = REPORTSLOCATION;
$curDir = getcwd();
chdir($reportLocation);
if (file_exists($filename)) {
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($filename));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
readfile($filename);
}
chdir($curDir);
The trick is to check with file_exists to confirm the correct path.
The big confusion is that for PHP paths you don't need to start with '/' to say your website start path.
'/folder/file.ext' #bad, needs to know the real full path
in php / is the website main path already, you don't need to mention it. Just:
'folder/file.ext' #relative to the / website
This will work with file_exists, header filename, readfile, etc...
if script work work for small files but for huge files return 0 byte file. you must increate memory_limit via php.ini
also you can add the following line before your code
ini_set('memory_limit','-1');