How to download a php file without executing it? Ask Question. Asked 10 years, 5 months ago. Active 2 years, 1 month ago. Viewed 64k times. Improve this question. Jagadeesan Jagadeesan 1, 3 3 gold badges 9 9 silver badges 24 24 bronze badges. Download from where? Via what? Download from where to where? Please add more detail — Pekka. See my answer for a working example.
Add a comment. Active Oldest Votes. Improve this answer. This works. But you shouldn't really be doing it this way - PHP is not designed to handle file downloads this won't support resuming, capping and has a large overhead use something which is designed to handle this problem: a file server. However this is what OP asked for. Yeah totally, was just making sure he was aware of the potential issues! Anders Abel Anders Abel If such kind of file is stored in a public accessible folder, you can just create a hyperlink pointing to that file, and whenever a user click on the link, browser will automatically downloads that file.
Clicking a link that points to a PDF or an Image file will not cause it to download to your hard drive directly. It will only open the file in your browser. Further you can save it to your hard drive. However, zip and exe files are downloaded automatically to the hard drive by default. You can force images or other kind of files to download directly to the user's hard drive using the PHP readfile function.
The syntax of this function is given below. This function can take three arguments. The first argument is mandatory, and the other two arguments are optional. This function returns the number of bytes read from the file mentioned in the first argument. The uses of this function are shown in the following two examples. In this example, we will create an HTML file with the following code, where the file name will be passed as a parameter of the URL named path , and the value of this parameter will be passed to the PHP file named download.
We will create the PHP file with the following code to download the file forcibly. Next, the header function is used to set the necessary header information before using the readfile function. The basename function is used to retrieve the filename, and the filesize function is used to read the size of the file in bytes, which will be shown in the opening dialog box to download the file. The flush function is used to clear the output buffer.
The readfile function is used with the filename only, here. Output The following output will appear after clicking the download link of the image file. The file size of the rose.
0コメント