content type may be? - php

I have an xml file saved and this is what I want to do,
when some one click the link that contains an xml file, I want them to ask the question if they want to save or open the file.
I know there is a way to do this using http header to send and trick your brower into using the application/octet-stream mime type, but I forgot how it was done. ..
it gives me this error
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
XML document must have a top level element. Error processing resource 'file:///C:/Documents and

Try adding Content-Disposition header:
Content-Disposition: attachment; filename="yourfile.xml"
And of course, the XML Content-Type header also:
Content-type: text/xml

Related

How can I tell the filetype of images without extension? How do they work?

I am dealing with images on the web that come without a file extension, like this:
https://static1.squarespace.com/static/52a74d9ae4b0253945d2aee9/t/52ed63b1e4b04368c021b921/1463088116168/?format=500w
Images like these can be found, e.g., on websites made with squarespace, like this demo: https://bedford-demo.squarespace.com/
I'm trying to download these images and store them on my server, using PHP. But how can I find out the actual URL of those images? How does this work? And how can I tell the filetype of this image? What is this sorcery?
Any hints are appreciated!
Quick answer:
To find out the Content-Type returned for any URL, look at this answer:
Get Content-Type of requested URL in PHP
Why you need the Content-Type:
Just like how not every webpage on the internet has an URL that ends with .html, images are not required to have an "extension" in their URL either.
What determines whether the browser will treat the data retrieved from the URL as an image is the Content-Type header in the HTTP response.
The URL you posted returns the following HTTP headers:
For HTML documents the Content-Type is text/html. You can inspect the headers as you browse by opening the Network tab of the developer console in your browser. Look for the "response headers".
You can get the mime type of the file with getimagesize:
<?php
$size = getimagesize("https://static1.squarespace.com/static/52a74d9ae4b0253945d2aee9".
"/t/52ed63b1e4b04368c021b921/1463088116168/?format=500w");
print_r($size["mime"]);
?>
Prints:
image/jpeg

Browser just downloads page when I set the header to xml/text

I have a php file where everything works fine, but If I set the header to:
header("Content-type: xml/text");
Like it should be, my browser just tries to download it. It works with curl, but not Safari. It's weird because I have a lot of files that have that header on my server and they work, I even created a test php and set the header to that and it worked. How can I debug why that specfic page downloads when I set the header?
Try
header("Content-type: text/xml");
text/xml is correct mime-type. Different user agents likely handle the invalid xml/text mime type differently, resulting in weirdness.
(More precisely, use text/xml if there is some intention that the XML could be readable by a human and use application/xml if not. Both these two are valid mime types for XML.)

What do I put under content-type in my header using php?

I have been using PHP for some time and I am trying to standardize my pages in terms of what they serve such as: text/html or json or possibly serving a file.
What I don't understand, when setting headers, is the "content-type" variable.
Is it possible to set MULTIPLE things with "content-type" or does "content-type" exclusely refer to the MIME type you are setting for the page?
Here is what I am currently setting but I don't know if there is anything else I should set with it:
<?php
header('content-type: application/json');
?>
This may sound "novice" but would help.
Thank you.
The content-type is specified per page, and you can and should set only one per page.
It tells the browser what type of page or request it's receiving so it knows how to render the output.
w3 - Header Field Definitions
When setting the content-type header you are telling the browser which MIME type to treat the following output as. You would use this if, for example, your PHP was not generating an HTML output but instead an image or audio file.
What is a Content-Type?
A "Content-type" is simply a header defined in many protocols, such as HTTP, that makes use of MIME types to specify the nature of the file currently being handled.
What is a MIME type?
MIME stands for "Multipurpose Internet Mail Extensions. It's a way of identifying files on the Internet according to their nature and format. For example, using the "Content-type" header value defined in a HTTP response, the browser can open the file with the proper extension/plugin.
Source: MIME Types List

Downloading file with PHP. How to get FILENAME?

I need to download files from remote server with my PHP script. The problem is that the links I recieve look like "example.com?download=12345". So I want to save the file with correct extension and (at best) preserve it's original filename. How do I do this?
Thank you all!
The Content-Disposition header in the HTTP response, if it exists, will contain the filename the other server wants the file to have.
#EricLaw (of the IE Team) has posted a detailed analysis of this on the IEBlog last November. While his post is focused on IE, the basics hold for most browsers:
use a Content-Disposition header (cleanest, standard-compliant, allows you to specify the filename; can be combined with either of the following)
don't use a query-string (not always practical - but possible with URL rewriting)
"Add a bogus querystring parameter at the end with the desired extension" (a bit of a hack, but works)
However, the source server is not obliged to do any of this; the most likely option would be the Content-Disposition header. If that is not present, you are back to square 1 (although you could guesstimate the file type from its content).
If you are using curl, the response header should return the actual filename. The header for file downloads looks like:
'Content-Disposition: attachment; filename="downloaded.pdf"'

Is there a way to force the user to download a file from a href link rather than to open it in a browser window?

Basically I wrote a script that generates a xml file based on user input. After the file is generated a download link appears like so:
Download File
But when clicked it opens the xml in the browser, I want it to start downloading when the link it clicked instead. Is there any way to achieve that?
Yeah, there is. It does require specifying some headers. Exactly how it works depends on what language you're using, but here's an example using php, taken off of php.net:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
Basically, first we tell the client what type of file we're sending, then we tell the client that what we're sending is an attachment, and it's name, instead of it being a page to display, and then finally we print/read the file to the output.
Given that you're already using php to generate the xml file, I would suggest adding the header commands above to the code that generates the xml file, and see if that does the trick.
If you happen to be using Apache for your web server, and you always want to force downloading of XML files, there is a more efficient way to do what #chigley suggested. Just add the following to a .htaccess file.
<Files *.xml>
ForceType application/xml
Header set Content-Disposition attachment
</Files>
What happens when a browser sees a link is not dependent on the link, but rather on the target of the link. Your web server should send the appropriate header: Content-Disposition: attachment;filename="file.xml" to tell the browser that it should prompt to save the file instead of displaying it.
It depends on what the client computer does with XML files. If you doubleclick on a XML file, it will open in your browser probably.
download.php:
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="file.xml"');
readfile('/path/to/file.xml');
HTML:
Download

Categories