Php force file download save as dialog
With that thought in mind, let's start out with this:. Okay, that should do it. The script first defines a variable to hold the path where the files are stored. Then fills an array with the file names of those files in that directory.
Next, it creates an array of file extensions and their corresponding content types. Next, it checks to see if it got a parameter telling it what file to download. If not, it generates an error. Next, it checks to see of a received parameter matches any of the files in that directory.
If everything is okay so far, it gets the file extension of the requested file and then checks to see if it has a content type to match it. If everything is still alright, it get the file size, opens the file. If that's successful, it sends the needed headers of Content-Type, Content-Length and Content-Disposition, then uses the PHP fpassthru function to send the file to the browser.
It then closes the file and exits. If anything has gone wrong throughout the process, it sends the error message to the browser instead. It's all pretty simple, really. Now, let's take it a step further.
We're going to create a directory called downloads. Save the above code in that directory as index. Create a subdirectory under that downloads directory called files. Here is an example to download an Adobe Acrobat file named HelloWorld. Here is a similar link to a. Now, as slick as the above is, it's still just the slightest bit messy. It still has those nasty url parameters in the link. If you can use a. The following. What this. So it will take that request for HelloWorld.
Now look what we can do. It looks just like any other link, but still generates the download dialog. Here is the. I'm not sure if this needs to be worked in to this file or savefile. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 8 years, 11 months ago. Active 8 years, 11 months ago. Viewed 10k times. I have a file, index2. Add a comment. Active Oldest Votes. Mitch Satchwell Mitch Satchwell 4, 2 2 gold badges 21 21 silver badges 31 31 bronze badges. Archived Forums. Internet Explorer Web Development. Sign in to vote. We are having an issue with forcing a download of an MP3 in Internet Explorer 6 and sometimes 7.
This is what is happening. A user clicks on a button in a Flash Application to initiate a call to a file called download. The Save As dialog box appears correctly, but instead of downloading the MP3, it tries to download whatever is at the end of the URL, directory name or file name. In every other browser, it will initiate a download and download the MP3 correctly, but in IE it will not download the MP3 and will just timeout.
At the Save As dialog box, if you click on Open it tries to open in Windows Media Player, but fails because it is trying to download a file that doesn't exist. If you click Save, it times out because it is trying to download a file that doesn't exists.
0コメント