How to make file downloadable in php

By Gozahn | 14.06.2021

how to make file downloadable in php

How to Download a File in PHP

How To Make Files Downloadable With PHP. you may want usera€™s browser prompt to download the file instead of opening it in the browser window or in an external program. There are a few Estimated Reading Time: 1 min. Summary: in this tutorial, you will learn how to download a file in PHP using the readfile() function.. Introduction to the PHP readfile() function. The readfile() function reads data from a file and writes the it to the output buffer. Here’s the syntax of the readfile() function.

Normally, you don't necessarily need to use any server side scripting language like PHP to download images, zip files, pdf documents, exe files, etc. 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, yo 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 maoe hard drive using the PHP readfile function. Here we're going to create a simple image gallery that allows users to download the image files from the browser with a single mouse click. If you see the above example code carefully, you'll find the download link pints to a "download.

Also, we've used PHP urlencode function to encode the image file names so that it can be safely passed as URL parameter, because file names may contain URL unsafe characters.

The regular expression in the above example line no-8 will simply not allow those files whose name starts or ends with bow dot character. Please how to make paper bells out the tutorial on regular expressions to learn the regular expressions in details. Is this website helpful to you? Please give us a likeor share your feedback to help us improve.

Connect with us on Facebook and Twitter for the latest updates. Example Try this code ». Example Run this code ». Previous Page Next Page. All Rights Reserved. Share This:.

You can leave your ad blocker on and still support us.

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. Syntax. int readfile (string $filename [, bool $use_include_path = false [, resource $context ]]) This function can take three arguments. // File to download. $file = '/path/to/file'; // Maximum size of chunks (in bytes). $maxRead = 1 * * ; // 1MB // Give a nice name to your download. $fileName = 'download_usloveescort.com'; // Open a file in read mode. $fh = fopen($file, 'r'); // These headers will force download on browser, // and set the custom file name for the download, respectively. header('Content-Type: application/octet-stream'); . Sep 30,  · http response headers to set composition and file to download. header ('Content-Disposition: attachment; filename="usloveescort.com"'); The length of the requested file need to download. header ("Content-Length: ". filesize ("usloveescort.com")); Reads a file and writes it to the output buffer. readfile ('usloveescort.com');Estimated Reading Time: 1 min.

It was previously mentioned that zip and exe files download automatically, without using PHP script. First, create an HTML file with the following code.

Here, the four anchor elements are defined to download the four types of files. Output The following dialog box will appear to download the file after clicking the zip file link. The user can then download the file or open the file in the archive manager. If you click on the image file, the image will be opened automatically in the browser, as shown in the following output.

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:. We will create a PHP file with the following code to download a file from the file path. The PHP code in the previous example will be slightly modified to download the file from the given path.

The clearstatecache function is used to clear the cache that was previously stored. Two arguments are used in the readfile function. Output After the download link of the PDF file is clicked, the following output will appear.

This article provided a simple way to forcibly download any file using the PHP script, to help readers to add the download feature in their script.

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. Generally, no PHP script is required to download a file with the extensions exe and zip. If the file location of this type of file is set in the href attribute of the anchor element, then the file automatically downloads when the user clicks on the download link.

These files can be downloaded forcibly in PHP using the readfile function that does not download automatically.

This tutorial shows you how to forcibly download any file using PHP script. Check Download Links It was previously mentioned that zip and exe files download automatically, without using PHP script.

Fahmida Yesmin I am a trainer of web programming courses. View all posts.

2 thoughts on “How to make file downloadable in php

Add a comment

Your email will not be published. Required fields are marked *