Provide a file to download in website php






















In this article, we are going to show how to download a file from directory or server in PHP. Using header and readfile function, you can easily download a file in PHP. Also, this simple PHP script helps to implement a download link which downloads a file from the directory. The following example script can be used for download any types of file like text, image, document, pdf, zip, etc.

Sometimes you need to provide a link to the user for download file from the server. HTML Code:. Are you want to get implementation help, or modify or enhance the functionality of this script?

Submit Paid Service Request. If you have any questions about this script, submit it to our QA community - Ask Question. Thank you dear, its really helped me alot. Your Website. Toggle navigation. Ask Question Share Tweet.

You must save the file to make a copy of the image file in the local drive. In the same way, when you click on PDF and TEXT file links, the content of the file will be opened in the browser without downloading the file. The solution to this problem is to download the file forcibly using the built-in PHP readfile function. The readfile function is used in PHP script to forcibly download any file of the current location, or the file with the file path.

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. You can download the file by selecting the Save File radio button and pressing the OK button.

If the file exists at the given file location, the file path will be required to mention in the URL. In this example, we will create an HTML file with the following code, which will pass the filename with the file path:.



0コメント

  • 1000 / 1000