I try to use php to force the image jpg file download, I have implemented eth following code:
html
<a href = "filedownload.php?src=uploads/myimage.jpg&download=true>download this file</a>
download.php
<?php
ob_start();
include_once 'functions.php';
if (isset($_GET['download']) && $_GET['download'] == 'true')
{
$src = sanitizeString($_GET['src']);
header('Content-Description: File Transfer');
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename='.basename($src));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
}
?>
Suppose the full path of the image is "www.example.com/smith/topic/uploads/myimage.jpg", I have recieved the right image name and the download window is appeared as well, but the image is corrupt and with 1KB size, any one could tell me why, thanks a lot.
Here you are example how to use readfile function
<?php
$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;
}
?>
You need some code that actually sends over the file. See e.g. readfile or fpassthru
Try to use:
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
Related
i want to create a download pdf file, but its always damaged
i had try this code in other function its working correctly but in this function the downloaded file is corrupt
source and downloaded file has same size its 3.899
this is my code before :
if (file_exists($dir)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($dir).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($dir));
readfile($dir);
exit;
}
and i had try this code too for this issue :
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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: ' . $length);
set_time_limit(0);
readfile($file);
exit;
i think the problem is in file size,
my code is work corretly in size under 1Mb but i dont know how to change max download size, i had try to search in php.ini but i cant find
====> update
i had try to change file to smaller size file (below 1Mb) but its still damaged
My situation (all in PHP):
What I get: A Base64 encoded string from an API.
What I want: A link to click on that downloads this document as an PDF. (I am sure it will always be a PDF file)
I have tried this:
$decoded = base64_decode($base64);
file_put_contents('invoice.pdf', $decoded);
but I am kind off lost, can't seem to find a way to download it after decoding it.
I hope someone can help me, thanks in advance!
The example here seems helpful: http://php.net/manual/en/function.readfile.php
In your case:
<?php
$decoded = base64_decode($base64);
$file = 'invoice.pdf';
file_put_contents($file, $decoded);
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
This should force the download to occur.
Don't need to decode base64. You can send header with binary file.
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($filedata));
ob_clean();
flush();
echo $filedata;
exit;
I need make xlsx file download from my site (but not from directly open file url like this: http://site.com/file.xlsx )
So, this is php code
$file = "somefile.xlsx";
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
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);
file is downloaded, his extension is .xlsx, but when trying open this file in ms excel, file not opened and I got error : excel cannot open the file.xlsx because the file format or file extension is not valid
Tell please, why this happened? where I am wrong?
After many years, I got same problem, and after searching, I got here again ))
This is solution, that worked for me:
$file = "somefile.xlsx";
// define file $mime type here
ob_end_clean(); // this is solution
header('Content-Description: File Transfer');
header('Content-Type: ' . $mime);
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($file);
You must be using this code in middle of some other file.
The problem with headers is they need to be set first on a page. They will not work if you have even 1 single space echoing before them. So you need to ob_clean() [clean the buffer] before you are setting headers
Try
ob_clean();
flush();
$file = "somefile.xlsx";
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
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));
readfile($file);
Remove:
ob_clean();
flush();
Add at the end of code:
exit();
The issue is that flush() will also throw in your *.xlsx file content some garbage it has in it and that will corupt your file, even if you use ob_clean();
For a better understanding go to php.net and read the difference between flush(), ob_flush() and find that you didn't even need them in the first case. Therefore you won't need the ob_clean() too.
This works for me:
header('Content-Description: File Transfer');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"".basename($fileLocation)."\"");
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Length: ' . filesize($fileLocation)); //Remove
ob_clean();
flush();
readfile($fileLocation);
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);
I was trying to write a script through which user can download the image directly.
Here is the code i end up with,
<?php
$fileContents = file_get_contents('http://xxx.com/images/imageName.jpg');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.urlencode("http://xxx.com/images/imageName.jpg"));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileContents));
ob_clean();
flush();
echo $fileContents;
exit;
?>
But everytime i hit the url for the above script in the browser it returns a file with zero byte data.
would you like to help me to resolve this problem ?
Try the code below
<?php
$file_name = 'file.png';
$file_url = 'http://www.myremoteserver.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
?>
Read more , Read this tutorial too
I notice that you use filesize on the contents of the file instead of at the filename itself;
Your code would work if it was:
<?php
$filename = 'http://xxx.com/images/imageName.jpg';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.urlencode($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_clean(); // not necessary
flush(); // not necessary
echo file_get_contents($filename); // or just use readfile($filename);
exit;
?>