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 .
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 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
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.
I'm trying to create a download link for a PDF file. It works fine if Adobe Reader is installed, but if I uninstall it, it tries to open in browser and fails.
Please tell me what is the actual problem about this?
Thanks in Advance.
The problem is that when adobe reader is installed than it automaitcally gets the header of .pdf file and show the file.
You can use this.It will always prompt to download the file ..
$path="uploads/";
$actualfilename=$path.$row["name"];
//name of the file or location of file..
if($typeofview=="download") {
#readfile($actualfilename);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $actualfilename. '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($actualfilename));
header('Accept-Ranges: bytes');
exit;
}
This is a duplicate of this question but here is the answer:
$path_to_file = '/var/www/somefile.pdf'; //Path to file you want downloaded
$file_name = "somefile.pdf"; //Name of file for download
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-Transfer-Encoding: binary');
readfile($path_to_file);
die();
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)."\";" );