Download Xml File Php

  

How to use PHP and the Content-disposition HTTP header to force files to download that would normally open in the web browser and display inline. Hello, First, I am very new to php. Bloody Beginner. My problem is, that i want the user to be able to download a generated xml file. I have working code which savies it to a file and sends it. The PHP documentation has lots on XML. The only problem I can see that you might have with external files is that you may have to use cURL to get them.

Active1 year, 9 months ago

I created xml file in php. This file is successfully saved in my server directory.
Now, I want to download this file only through php, not javascript.

How can I save this file?

Nick Craver
551k117 gold badges1219 silver badges1118 bronze badges
DownloadLayricLayric
492 gold badges5 silver badges12 bronze badges

1 Answer

Eduard7Eduard7
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Not the answer you're looking for? Browse other questions tagged phpxmlfbjs or ask your own question.

Xml

Because most modern browsers display PDF and media files inline, use the PHP programming language — which allows you to change the HTTP headers of files that you’re writing — to force the browser to download rather than display a given file type.

You'll need PHP on the web server where your files will be hosted, a file to be downloaded, and the MIME type of the file in question.

How to Use PHP to Force a File to Download

This process requires two separate steps. First, you'll create a PHP file that governs the file you wish to protect, and then you'll add a reference to that PHP file within the HTML of the page in which it appears.

After you upload a file to the server, create a PHP document in a text editor. If, for example, you wish to force sample.pdf to download instead of to display inline, create a script like this:

The content-type reference in the PHP is important — it's the MIME type of the file you're protecting. If, for example, you saved an MP3 file instead, you'd need to replace application/pdf with audio/mpeg.

There should be no spaces or carriage returns anywhere in the file (except after a semi-colon). Blank lines will cause PHP to default to the MIME type of text/html and your file won't download.

Save the PHP file to the same location as your HTML pages. Then modify the page's link to the PDF as follows:

Php Auto Download Xml File

Considerations

How To Open Xml File

Two important considerations govern this procedure. First, if someone discovered the direct link to the PDF file, he or she can access it directly without the PHP getting in the way. Second, you'll need PHP protection for each file you wish to protect using this quick-and-easy approach. To protect several files in this manner, it makes sense to name the protected file and the PHP file with the same name, differing only in the extension, to keep everything straight.