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.
Related
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'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
This question is for those who have used PHP library FPDF (http://www.fpdf.org ) to generate PDF documents using PHP. I am generating a PDF file using the php file 'my_file.php'. I want users to be able to download that PDF file. But in the browser the see the file in the address bar as ..somepath..../my_file.php . I want them to see it as a file with .pdf extension. Any idea how this can be done ?
when you create the object and then try to make output like this
$filePath = "files/cache/myPdf.pdf";
$pdf=new FPDF('p');
...
$pdf->Output($filePath,'I');
you can change and send the file name
To force download:
$pdf->Output('D'); //Force download and set filename as 'doc.pdf'
or setting your own filename:
$pdf->Output('MyFilename.pdf','D');
Your browser shall not open another tab whit yourpath/my_file.php
You can't change the browser address bar, but you can change the address on your server. For example if you're using Apache, there's mod_rewrite which allows you to do such things.
If your problem is that when downloading the file, the browser wants to save it as .php, you could use those headers to force the download and a filename.
header('Content-Type: application/octet-stream');
header('Content-Length: ' . FILESIZE_HERE);
header('Content-Disposition: attachment; filename=' . FILENAME.pdf_HERE);
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
My website writes 3 small text files based on users information and then presents these 3 files as links that they must "right click" and save to their desktop.
I would like to keep that, but also somehow offer a way to zip these 3 small files up and force download. And I don't want to save the zip file on the server either. Can this be done and how?
Thanks!
For the forced download you need to send out the file headers first.
header('content-type: application/zip');
header('content-disposition: inline; filename=YOUR_ZIP_FILE_NAME_HERE.ZIP"');
For zipping you'll wanna use one PHP's zip libraries, then echo/output the zipped content.
http://ca3.php.net/manual/en/zip.examples.php
Something like this:
$zip = new ZipArchive();
//the string "file1" is the name we're assigning the file in the archive
$zip->addFile(file_get_contents($filepath1), 'file1'); //file 1 that you want compressed
$zip->addFile(file_get_contents($filepath2), 'file2'); //file 2 that you want compressed
$zip->addFile(file_get_contents($filepath3), 'file3'); //file 3 that you want compressed
echo $zip->file(); //this sends the compressed archive to the output buffer instead of writing it to a file.