Php export text file to download






















Viewed 62k times. Add a comment. Active Oldest Votes. Mihai Iorga Mihai Iorga Thx I've tried it works but if I want the web browser to propose to download 2 files how should I do? To note that if you are using ajax dataType: 'json' to post data and call the PHP file, the content of the text file will be returned as text regardless what you put in your PHP code. Thnx but unable to fetch data in loop. Its showing only single data in txt file.

How should I do? Show 1 more comment. Edson Medina Edson Medina 8, 3 3 gold badges 39 39 silver badges 49 49 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Who owns this outage? Building intelligent escalation chains for modern SRE. 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.



0コメント

  • 1000 / 1000