Download image from server ajax uploaded from javascript - php

I currently user html2canvas to get a div as an image.
html2canvas([document.getElementById("board")],{
onrendered: function( canvas ) {
var img = canvas.toDataURL();
var request = $.ajax({
type:"POST",
url:"downloadPic.php",
data:{downloadPic : img}
});
Then, I want to download that image (to the browser default download directory)
if(isset($_POST['downloadPic'])){
$pic = $_POST['downloadPic'];
$filteredData=substr($pic, strpos($pic, ",")+1); //html2canvas adds this
$unencodeData=base64_decode($filteredData);
$filename = "downloadedPic";
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
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($unencodeData));
ob_clean();
flush();
readfile($unencodeData);
}
But this doesn't seem to work. If I do a file_put_contents("downloadedPic", $unencodedData) the picture is saved on my server successfully.
Do I need the header's config differently?

Related

Image download - image cannot open issue

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;

PHP force download base64image

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 .

download a file from folder + php (PDF,JPG,PNG)

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

Save canvas image as image file and force download - fail to open download dialog

I'm trying to save canvas as a image file ,users get to determine which image format to download (png or jpg), then force download the file without storing the file on the server.
This is what I got so far:
JS Script:
$.ajax(
{
type : "POST",
url : "../php/download_image.php",
data:
{
format: 'png',
dataURL: flattenCanvas.toDataURL('image/png')
}
});
PHP:
$data = $_POST['dataURL'];
$format = $_POST['format'];
$file = $file = md5(uniqid()) . '.'.$format;
$uri = substr($data,strpos($data,",")+1);
file_put_contents($file, base64_decode($uri));
if($format == 'png')
{
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: image/png');
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));
readfile($file);
exit;
}
else {
echo "$file not found";
}
}
The code cannot force download and I have no idea why it is not working.
Any help greatly appreciated.
If you don't want to store the file on the server, you don't need to interact with the server in any way. Just let the user download the content of the canvas as described here.

file corrupt when click download link

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)."\";" );

Categories