Hello
How can I download a file in PHP?
I want when I click on submit button, it shows download dialog.
How?
Thanks
Printable View
Hello
How can I download a file in PHP?
I want when I click on submit button, it shows download dialog.
How?
Thanks
to force a save dialogue:
PHP Code:<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
OK, what if it's "zip" or "exe" file?
you should try looking up mime types, to make sure. there's a large list for application mime types here, so take your pick.
The filename tag under "Content-Disposition" is a recommended filename, and is what will show up in the user's save dialogue. The readfile() functionis the actual file path to the file the user is requesting.
It didn't work.
Actually its size is 7 M.B, but it said that its size is 1.90 M.B and the icon of the file is unknown.
Why?
.. post your code. I can't help you if I don't know what you're doing.
and just so you know, you don't need to do this to download an exe or zip file.. just make a link to them, the browser will automatically prompt to download..
Look at the dialog, there is something wrong.PHP Code:header('Content-type: application/exe');
header('Content-Disposition: attachment; filename="file.exe"');
readfile(file.exe');
What is wrong? The browser cannot know the icon of the exe, since it hasn't started downloading it yet.
all EXEs will show up with that icon when downloading, because the icon for that file is embedded in the exe.. and since they haven't downloaded the exe yet, it won't show up, just like CornedBee said.
however, you might be able to assign an icon file on your server to that file, but I have no idea how you'd go about doing it, and it has nothing to do with PHP, but rather how the web server is configured.
But after downloading, the file isn't working.
then it probably has something to do with the exe, and not the script. it might be corrupt, or you might not have the dependencies installed that it needs to run.
I used the same script and the file I downloaded ran fine.
PHP Code:<?
header('Content-type: application/exe');
header('Content-Disposition: attachment; filename="suggestedfilename.exe"');
readfile('filename.exe');
?>
I also tried zip files.
It isn't working after I'd downloaded it.
And the file is not corrupted because when I download it classically, it's working.
post the code you're using?
PHP Code:header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="suggestedfilename.zip"');
readfile('filename.zip');
try using the absolute file path to the file you want? ie: /home/user/files/filename.zip (unix) or C:/filename.zip (windows)
The code is fine, so unless you have more code that might be making it not work, I'm not sure what to tell you.
Same problem
then it's something to do with your browser or the server, I'm guessing.
There is a code I've just found, it's
And it worked.PHP Code:header ("Location: file.exe");
What the difference between it and the code you gave to me.
The code just forwards the browser to the actual location of the server-side file. It won't work if that location is not directly accessible or if you want to generate the file on the fly.
I didn't understand.
Can you explain more for me pleeeeeeease?
What you pasted just redirects the page to that file, and is only accessible if you have it available on your HTTP server. However, using the method I posted, it's possible for you to download files on the server, but not in the HTTP directories (I believe so, anyway @_@). Basically, the script you made is nothing more than just like creating a link to the exe file :/
OK, why didn't the code you posted work?
please
it worked fine for me. I don't know why it wouldn't work for you. It most likely has something to do with your web server's configuration.
Does anyone know what I have to do to make it work?
pleeeeeeeease.