i am using the following code
<?php
$rFile = $_GET['sfile'];
$rExt = $_GET['ext'];
header("Content-disposition: attachment; filename=".urlencode( $_GET['sfile'] ).".".$_GET['ext']);
header('Content-type: Image/jpeg');
readfile( "wallpapers/".$rFILE.$rRes.$rExt );
?>
i pass the variables and the files are being downloaded.. but the images are not being created ..
i see a image file with image icon with correct name but theres no preview
the images are not opened in any image viewer (all the images i use are JPEGs)
all the downloaded images have same size 336 to 337 bytes... so i thought maybe transfer size is limited and so i use Similar SO Question but after that only the file size grew to 445bytes to 446bytes nothing more...
WHAT TO DO.. i need to provide direct links for image download...
I'll bet a beer that the 336 to 337 bytes contain a PHP error message that will tell you what the problem is.
Remove the JPEG content-type header to see the output.
at line 1 you're witing $rFile, in line 5 it's $rFILE - maybe thats the problem? if not, it's impossible to say without seeing the error-message.
I hope that you are doing some sanitizing of $_GET data or you'll have a lot bigger problems than image data not downloading correctly. That code is ripe for a directory traversal attack.
okay now i did this
$rFile = $_GET['sfile'];
$rExt = $_GET['ext'];
header("Content-disposition: attachment; filename=".urlencode( $_GET['sfile'] ).".".$_GET['ext']);
header('Content-type: Image/jpeg');
readfile( "wallpapers/".urlencode($rFile).".".$rExt );
now the files are being download with correct size.. but i still can't see the preview.. my image veiwers & editors say this
ACDsee blank screen
Windows Page & Fax Viewer no preview available
MS Paint path Paint Cannot Read This File This is not a valid bitmap file. Or its format is not supported
Related
I've successfully implemented some PHP to download an image:
PHP:
<?php
$image = "path/to/image.jpg"
header('Content-disposition: attachment; filename=image.jpg');
header('Content-type: image/jpeg');
readfile($image);
?>
Here is an example image that I've saved to my server: http://www.thenally.com/work/php-tests/header/files/herzog.jpg
If I view the image in the browser, then right-click to save, it acts as a typical image on my local Windows box (ie. viewable in Windows Photo Viewer). When I download the image using the PHP script above, the image can't be viewed in the same way, although the filesize is accurate and I can open it in Photoshop. I don't know much about image encoding/decoding etc but obviously the file changes when I download using the method above.
EDIT:
The Windows Photo Viewer error is as follows:
"Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or you don't have the latest updates to Photo Viewer".
There are no PHP errors in my log file, but this came up in my Chrome console:
"Resource interpreted as Document but transferred with MIME type image/jpeg: "http://thenally.com/work/php-tests/header/index.php".
try this
header('Content-Transfer-Encoding: binary');
I ran a similar issue and managed to fix it by removing any blank space/new lines between PHP tags.
Basically had to do this:
<?php
//stuff
?>
--REMOVE THIS SPACE--
<?php
//stuff
?>
Just make sure you do not have any blank spaces before any PHP tags and you should be good!
First post. I'm working on a project for a client where they have pdf files uploaded to a file structure (LAMP Stack) but the files have no extensions on them. Under the assumption that those files have to be PDF how would I get the browsers to understand that, and open them accordingly? Obviously with adding the file extensions this would suddenly work but I can't change the way their system works, it would result in too many changes and they are on a tight deadline. As for saving a temporary copy somewhere, I could do that, but I was hoping for a better solution. Is there a way to suggest to the browsers that they open a file a certain way?
Any thoughts guys/gals?
You just set the application type and file name in the headers, like so:
// This points to the file in question, note that it doesn't
// care whether it has an extension on the name or not.
$filePathOnDisk = '/path/to/your/pdffile';
// You can make this whatever you like, it doesn't have to
// be the same as the file name on the disk! This is the name of the file your end
// user will see when they are asked if they want to save. open, etc in the browser.
$fileName = 'file.pdf';
$data = file_get_contents($filePathOnDisk);
header("Content-type: application/pdf");
header("Content-disposition: attachment;filename=$fileName");
echo $data;
See PHP: stream remote pdf to client browser and Proper MIME media type for PDF files for reference as well.
Tested
You can use the following which will prompt the user to save the (PDF) file on their computer.
Notice the different file names.
One is the file that will be uploaded/prompted to the user download_example.pdf, while the other is the file without an extension as set in readfile('example');
<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="download_example.pdf"');
readfile('example');
?>
I am trying to rename file and download it for user!
Example... In my website I offer to download Google Logo from my website link!
http://www.mywebsite.com/files/logo-mysite.jpg
then the file is saved as logo-mysite.jpg in the users computer but the file in real is downloaded from https://www.google.co.in/images/srpr/logo4w.png
We can do it by saving the image from https://www.google.co.in/images/srpr/logo4w.png temporary to our website and then auto-delete it on download or auto-delete within 1 hour!
You can suggest a filename for a download by adding a content-disposition header:
header('Content-disposition', 'attachment;filename=logo-mysite.jpg');
die(file_get_contents('https://www.google.co.in/images/srpr/logo4w.png'));
Obviously this would give problems since you are downloading a png file with a jpg extension, but I'm just following your question... To convert the image you'd need to add some lines of GD2 code.
Use the header() function to set the name that the user will download:
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename=logo-mysite.png');
// Open the new file, and dump it out to the user
$handle = fopen( "https://www.google.co.in/images/srpr/logo4w.png" );
fpassthru( $handle );
fclose( $handle );
If you really want to convert it from jpg to png, you'll need to run it through ImageMagick or the likes.
I'm currently displaying images and hiding the src code by having a php file output the image. But when I right click on the image displayed and go down to 'Save As' it prompts me to download the php file not the actual image (obviously because it points to that src).
What can I do to download the actual image instead of displayImage.php?
It doesn't prompt you to download the PHP file, it simply uses that as the file name, because that is the file name from which it got the image data. If you manually input a valid image file name and try to open what you saved, it should still be a valid image.
You may also be able to give it a sensible name by including the file name in a Content-Disposition: header from your PHP file, e.g.
$filename = 'image.jpg';
header('Content-Disposition: inline; filename="'.$filename.'"');
// Don't forget the Content-Type as well...
// Output image here
...however this relies on the browser handling this sensibly, which not all of them do :-(
You can send a filename in the header.
header("Content-Type: image/png");
header('Content-Disposition: inline; filename="some.png"');
Send the correct content type in the image generator script:
header('Content-type: image/jpg');
If you want to have the .jpg extension when a PHP script is outputting an image, you'll need to do a htaccess or httpd.conf rewrite, where you can rewrite a .jpg request, to your php image generator script.
See mod_rewrite http://httpd.apache.org/docs/current/mod/mod_rewrite.html
I am facing problem in downloading any image file from the server.
I am able to upload them successfully and can open them from the location where they are uploaded and stored.
When i download using my function the image files get downloaded fully, file size is also correct but when i open them i get an error No image preview !!!
$fileString=$fileDir.'/'.$fileName; // combine the path and file
// translate file name properly for Internet Explorer.
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
$instance_name = preg_replace('/\./', '%2e', $instance_name, substr_count($instance_name, '.') - 1);
}
// make sure the file exists before sending headers
if(!$fdl=#fopen($fileString,'r'))
{
die("Cannot Open File!");
}
else
{
header("Cache-Control: ");// leave blank to avoid IE errors
header("Pragma: ");// leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$instance_name."\"");
header("Content-length:".(string)(filesize($fileString)));
sleep(1);
fpassthru($fdl);
}
I am using IE as browser.
I am using this code snippet to download the file and not to show on the browser. The script executes and i get prompted on whether i want to open / save the file. When i save the file the size is also correct but the image doesn't show up. When i right click and see the summary of the file, it says the summary is unavailable.
Thanks in advance. Kindly help.
It's not clear from your code, are you using this PHP snippet to serve the image on a web page, such as:
<img src="my-php-script.php" alt="blah blah blah" />
If so, your content-type is incorrect. You would need to use an image MIME type, such as image/gif, image/jpeg, image/png or image/tiff, whichever is most appropriate.
This line: header("Content-type: application/octet-stream"); seems fishy to me. You might want to try giving the actual mime type and see if that helps
The problem is with the MIME type. Please have a look here:
http://www.daniweb.com/forums/thread122055.html
This is if you want to view images in the browser.
If you want to download them as binaries then leave your mime type.
Also please change file open mode from "r" to "rb"