I tried to create some code so that a user can download an image in laravel without right clicking and using Save as... but after I created this code and download the image, trying to open the image says the image was damage.
$file = urldecode($request->file);
$remoteURL = $base_url.$file;
//$fname = basename($remoteURL);
// echo $remoteURL;
// exit;
// header('Content-type: image/*');
header('Cache-Control: must-revalidate');
header("Content-Type: application/octet-stream");
header('Expires: 0');
// header('Content-Disposition: attachment; filename='.basename($remoteURL));
header('Content-Disposition: attachment; filename="'.basename($remoteURL).'"');
// header('Content-Length: '. filesize($remoteURL));
header('Pragma: public');
flush();
readfile($remoteURL);
// file_get_contents($remoteURL);
exit;
Related
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 have a problem in my code php
i make wallpaper script
After upload image the name of image is saved in the database and image saved in the file (images/)
i want after show image in page php and click download
i want to download image in desktop
but after click in download the image saved in desktop but But be empty
see this image : http://im51.gulfup.com/riod8Y.png
my code
<?php
$getpic = $db->query('SELECT *','wallpaper',$where = array(
'id',
'=',
"".$id.""
));
$row = mysql_fetch_object($getpic);
$file = 'images/'.$row->image;
if($_POST['download']){
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;
}
}
?>
I have the following code for creating thumbnails of the images in the database automatically when the are uploaded.
<?php
include('config.php');
$result = mysql_query("SELECT * FROM photos where caption='cars'");
while($row = mysql_fetch_array($result))
{
echo '<div id="imagelist">';
echo '<p><img src="'.$row['location'].'"></p>';
echo '</div>';
}
?>
Now whenever I click an image in the gallery I want it to open the image in a new tab and give me an option to download, I want the download code which will take in the photo id from the database and then give me the option to download that image only how to do it.
Try this :
$file = your 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;
mysql_* functions are deprecated in new version of php, So use mysqli_* functions OR PDO
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)."\";" );