creating an image download link - php

I use wordpress cms and I am working on a little script wherein visitor could upload image, resize and download. I am stuck in the download stage. I have already written most of the code but I am unable to connect these things together to make it work somehow. First among these is a resize.php which basically resizes the image and creates a jpeg file. Here is some relevant snippet from my resize.php.
<?php
//normal validation stuff happens here
//resizing stuff happens here
//here is last part of the code that creates the resized image
$filename = uniqid();
$file = 'uploads/'.$filename.'.jpeg';
imagejpeg($new, $file, 80);
imagedestroy($new);
Second is a download.php. Here is a code in my download.php. If you see a lot of question mark in the code, that means I am sure that these are the codes I am missing.
// ????????????
$FilePath = TEMPLATEPATH. '/resize/uploads/';
$final = $FilePath . $FileName;
$size = filesize($final) ;
header("Content-Disposition: attachment; filename=\"". $FileName ."\"");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Transfer-Encoding: binary");
header('Content-Description: File Transfer');
header("Content-Length: ". $size ."");
header('Content-Type: image/jpeg');
header("Expires: 0");
header("Pragma: no-cache");
ob_clean();
flush();
echo (readfile($final));
Third is an html link I have currently on the template page, again question mark for missing code. If someone clicks the link they are supposed to get the newly created resized image file.
DOWNLOAD'
I hope you got the gist of my issue. I am having a hard time trying to figure out how these three will work together to create a download link for the recently resized image. While I am struggling for a solution myself, Please help me point out the mistakes and suggest corrections. THANKS.
FINAL UPDATE : After a few hours of effort, I have this sorted now. I did not realize earlier that the only thing I was missing was the query string and the $_GET. After I understood their role in this whole process everything was pretty easy. Later on when I downloaded the images they came out corrupt. How I sorted that I saved the corrupted files everytime, opened them up in notepad++ and checked for the error. Now my application is flawless. Of course the codes are drastically changed, now. Thanks to everyone for whatever bit of interest they took to help.

Related

ERR_EMPTY_RESPONSE on downloading generated zip

So i was trying to generate a zip file with a bunch of pdfs in it.
After some time I got it working but the problem servers gives me ERR_EMPTY_RESPONSE.
Server's apache is 2.2 on windows server 2003...
PHP Version is 5.2.
This is the code:
<?php
$file_names=$pdfs; //This is an array of names of files in PDF format: 1.pdf, 2pdf, 3pdf...
$zip = new ZipArchive;
$zip_name = ("test.zip");
$path_zip = ("C:/www/test/docs/");
$zip->open($path_zip.$zip_name,ZipArchive::CREATE);
foreach ($file_names as $file) {
$zip->addFile($file,basename($file));
}
$zip->close();
//then send the headers to force download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$zip_name");
header("Pragma: no-cache");
header("Expires: 0");
#readfile($path_zip.$zip_name);
exit;
?>
THE PDFs ARE PREVIOUSLY DOWNLOADED FROM THE INTERNET WITH ANOTHER CODE
How do I fix this??
EDIT::
Adding a image showing the zip.
EDIT 2::
I removed the # at the last line and tested. Nothing works as intended.
So...
It's ugly but it works...
I just changed
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$zip_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile($path_zip.$zip_name);
exit;
for
header("Location: ".$path_zip.$zip_name);
As I say, it's pretty ugly, but it works, it make the user download the file and it doesn't get stuck or something like that.
If someone want to make another true fix, I will test it, as I don't want to change the location.
Bye.

PHP Download Problem: Not Successful w/ Slow Connection Speeds

I've been stuck on this problem for a few days, and have yet to find a solution that fixes the problem I'm having.
What I'm Trying To Do:
I'm attempting to use PHP to download PDFs, and the code works very well for files that can download within about a minute and a half. On my home wifi, I'm able to download a 159MB file within 10 seconds, and it works every time. But when I limit the internet speed to "Fast 3G" (around 170KB/s, in order to simulate slower office speeds), the download fails. And nearly every time, it does so exactly 3 minutes and 24 seconds into the download process, but occasionally it is a lower time of 1 minute and 57 seconds.
What I've Tried:
I've tweaked the php.ini file (setting max_execution_time = 0, and memory_limit at higher intervals than the originally configured 128M)
I've tried other download methods that seem to "chunk" the larger PDFs. This has been mostly unsuccessful. In one instance the download would complete, but there would be an error when trying to open the PDF. According to the poster of this solution, it was only a valid solution for UTF-8 encoded files, and I found the one's I'm dealing with to be UTF-16. (I believe it was some kind of incompatibility with the print() function.
I've made sure the file can download if using a direct link in the URL. It has no problems this way, but it was only done for testing, and cannot be a permanent solution because the PDFs I'm dealing with contain sensitive information. So based off of this result, I was at least able to narrow down the problem to be PHP related and not IIS.
Here's the current code I'm using
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header("Content-Transfer-Encoding: binary ");
header('Content-Length: ' . filesize($file));
//$file is a full path to the PDF
while(ob_get_level()) {
ob_end_clean();
}
readfile($file);
flush();
exit;
/*I realize it may be off, but it is at least working for quicker load
times as it currently is, so I'm leaving it alone for now*/
I tried to include any information that seemed relevant, but if any additional information would be useful please let me know! I will also be sure to include the current code that is handling the download process that I mentioned at the top of the post.
Instead of
readfile($file);
flush();
I would try
$handle = fopen($file, 'r');
while (!feof($handle)) {
echo fread($handle, 8192);
flush();
}
fclose($handle);
you may need to adjust the above to handle proper encoding, but that will depend on your environment

Why is my PDF download corrupted?

I have a script that generates a pdf using fpdf, this file is saved correctly on the server on my computer. In that same php file I run the following code to download the file. When i download the pdf and I check it in notepad, everything in the pdf is the same, except for the fact that it contains a lot of my previous files html at the beginning of it. The file saved to my server doesn't have any of that.
What could cause something like this to happen? I have no idea where to look for the source of this error, can anyone point me in the direction to finding the problem?
<?php
$filename=($name.$ran.'.pdf');
$pdf->Output($name.$ran.'.pdf');
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octetstream');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($filename));
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
readfile($name.$ran.'.pdf');
?>
I managed to figure things out, thanks to hakre! Essentially I just needed to clear out the output buffer using ob_clean () before using readfile() and the code worked!

create download link for php created image

I am working on a little resize and download image kind of php script wherein the events must happen in the following fashion :
A visitor uploads an image file.
The image is resized by the script.
A download link to the resized image appears in the front-end.
My resizing code is ready and working but I need help with the third part, that is how do I offer a download link to the recently created image. Here is a gist of how the resized image is created i the php script.
<?php
//normal validation stuff happens here
//resizing stuff happens here
//here is last part of the code that creates the resized image
$filename = uniqid();
$file = 'uploads/'.$filename.'.jpeg';
imagejpeg($new, $file, 80);
imagedestroy($new);
This download link must prompt as a download box instead of opening a new tab. Headers is what I know is needed in case we need to offer a download link. But this is useless to me unless I understand a right approach to this. Also please understand that my situation may be a little different as this is not a download link for a static-resource. Here the images to be downloaded are created dynamically at every request, everytime.
While I am struggling on my own a solution, will be great otherwise please provide me a road-map of what steps should be taken or what are functions that I will need to get this done. Thanks in advance.
Step 1 - User is uploading picture and you are saving it on server (you can resize it now) with new unique name (maybe md5 from time()?). On response page you can give link like this www.mypage.com/downloadimg.php?fileid=54gj6hg45h654g where last part is generated file name.
Step 2 - User clicks download link. You just have to send headers from Hanky 웃 Panky answer and then file_get_contents().
You can force a download using proper headers.
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: ' . filesize($file));

Force image download with php using header() in smartphones and tablets

I was searching already for a long time and I havent seen any right answer yet.
I'm trying to create a system in PHP where the user can download a signPicture that I create in JPG.
The program is working fine in all desktop computers. There is not problem at all, even for IE8.
The header that I use:
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="test.jpg"');
in the end i just stream the picture:
imagejpeg($imgSign,NULL,100);
How I said, it's working really good in every browser. But then we get to the mobile devices, where in android for example, download a test.jpg file... but then it cannot open... and the same with ipad (actually doesnt download, it show the image in the browser and than I save it... but it does not open either).
I also try more examples that I saw, but doesnt change anything, like:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Transfer-Encoding: binary ");
Any idea how to sort this out in mobile devices?
Thanks!
I got it!
There were differents problems. I found the clear solution in comments from this post:
http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="test.JPG"');
The important steps: I send everything with a form. The form, to make it work in mobiles, needs to have the target='_top' and the method='get'
It also make errors if the extention (jpg) is not in UPPERCASE and the file name is not between " ".
Now it works in all devices that I try by far. :)
Special thanks to Jörg Wagner, author of the post.

Categories