I have in my index.php a link like this:
Download
I need that when you click that link the download dialong opens to save the foto with a specific name like myfile123.jpg.
In my download.php I have this:
header('Content-type:image/jpeg');
$handle = fopen($_GET['url'], "rb");
while (!feof($handle)) {
echo fread($handle, 8192);
}
fclose($handle);
And while it retrieves the image, it just opens it in the same tab (instead of forcing the dialog).
Take a look on the PHP readfile example
Example from php.net:
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);
You need to add another header, in order to trigger the download, like:
header('Content-Disposition: attachment; filename="image.jpg"');
More info about the Content-Disposition header, here: https://developer.mozilla.org/es/docs/Web/HTTP/Headers/Content-Disposition
Related
I already asked this question, but not correctly, and I didn't know, that I can't execute a html page and start a download on the same page, but now I know. The main problem actually still remains, I can't get the CSV file to download, it always get the PHP self. The code is executed. I didn't find any information about CSV in the php.ini. I created the file directly before the download (in another page), so it's impossible, that the it doesn't exist.
<?php
if(empty($_POST['checkbox_put_it_in_CSV_too'])!=1){
$file=fopen('created_files/Rankrohad_P_2021-04-01.csv','w');
$filename='created_files/Rankrohad_P_2021-04-01.csv';
foreach($rankrohad as $line){
fputcsv($file,$line,"|","'");
}
fclose($file);
$fsize=filesize($filename);
if(file_exists($filename)){
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header('Content_Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: no-cache');
header('Content-Length: '.$fsize);
readfile($filename);
exit;
}
}
?>
The problem is with your headers. Try this:
if(file_exists($filename)){
$fsize=filesize($filename);
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: no-cache');
header('Content-Length: '.$fsize);
readfile($filename);
exit;
}
At Content-Disposition header I escaped the filename and I placed filesize function in IF.
I want to offer a PNG image to download only for some users, and the image should not be located anywhere, so the users should not see it's path.
That's why I inserted it to the download.php as a base64 image. The problem is, however the image is offered for download, but it shows 0 B size when downloaded.
$base64strImg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxoAAARjCAIAAABnqMWCAAAACXBIWXM...';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=myimage.png');
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(base64_decode($base64strImg));
exit;
Not sure where is the problem, if it can't handle big image or why it can't be downloaded.
I also tried this way:
header('Content-Disposition: attachment;filename="test.png"');
header('Content-Type: application/force-download');
echo base64_decode($base64strImg);
Now the image has correct size, but still, can't be opened after download. The image viewer says unsupported format.
And third option - without decoding, it also has a correct size, but still can't be opened.
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename=test.png');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($base64strImg));
ob_clean();
flush();
echo $base64strImg;
exit;
According to advice from #Lawrence I updated my code and this way it works:
$resultimg = str_replace("data:image/png;base64,","",$base64strImg);
header('Content-Disposition: attachment;filename="test.png"');
header('Content-Type: image/png');
echo base64_decode($resultimg);
you don't have to do all that you can just upload image and restrict access to it by htaccess or permissions and use readfile with the headers in download.php and check if the user has the permission to download the file .
When I click on the file, I can view it in browser. When I click download, it says "failed to load the pdf document".
$file = "uploads/14204-2-002.pdf";
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file);
header('Pragma: public');
header('Expires: 0');
header('Content-Type: $mime');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.basename($file).'"'));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Length' . filesize($file));
ob_clean();
flush();
readfile($file);
Try this one(HTML5 and no Internet explorer).
In your HTML code you can use this line:
<a href="/images/myw3schoolsimage.jpg" download="w3logo">
Specify a value for the download attribute, which will be the new filename of the downloaded file ("w3logo.jpg" instead of "myw3schoolsimage.jpg")
Moreover you can read this post of this site:
Download files from server php
I am using the following code, to access a file which exists
$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('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
It downloads fine, but when opening the image viewer is refusing to open stating its not a gif file. Is
I think you should change the Content-Type to 'image/gif'
I found that there was a print elsewhere in the code so when it was forcing the download it was corrupted. Removing the print worked.
Hi I am trying to allow download a demo file which is related to a product. my code is like this
if(isset($_POST['submit'])){
$root = $_SERVER['DOCUMENT_ROOT'];
$path = "/download/";
$path = $root.$path;
$filename = $demo;
$file = $path.$filename;
header('Content-Type: application/force-download');
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Length: ".filesize($file));
readfile($file);
}
I am able to get file name which is associated with the product by
$demo
After user submits his information, download will start automatically. Now this is downloading a non existing file or corrupted file with the proper file name. please help
<?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;
}
?>
As you can see Content type is application/octet-steam meaning file is byte by byte encoded. Also the cache headers are set. Then headers are forcefully sent by ob_clean();flush(); and then the file is read.
The file_exists is there to ensure that given file exists. You should also try not not thrust user input as they could easy write names for your php codes and download EACH file. And with ../ in names of the files, even your documents or system files and so on.