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.)
Related
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
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
Hi everyone I am having a bit of a problem related to the php function file_get_contents.
I used it many times and no problems but when I am trying to get some information from a particular site the information I get when I echo the result is pretty much encoded (Example: ���IHDR�).
I looked at the header of the site and instead of saying
Content-Type: text/html;
it is saying
Content-Type: image/png
How do I decode that so I can get the source code (html) of the site? The web-site when I go to it in a browser, it looks like a regular web-site: text, images nothing out of ordinary.
When I look at the source code nothing out of ordinary there either. But when I do a file_get_contents I do not get the source code like I used to get on other websites.
Any ideas?
Note: I had the same problem in the past it was encoded in GZIP and I was able to find a function to decode it but with Content-Type: image/png I do not know how to proceed.
Why not, create a basic test script to the output the returned image, tho I suspect its an image saying:
Stop scrapping my site!!! Yada Yada
header('Content-Type: image/png');
echo file_get_contents('http://example.com');
The Content-Type header tells you which content-type the requested file has, in your case it is a PNG image (image/png).
You find a description of many content-types (written in a so called mime-type specification) online, this is a nice list: fileformat.info MIME types.
As you might can imagine, it's not possible to display an image in text-form (at least not before converting it to ascii art) so you will not have much luck this time.
Check the URI if it is really the one you wanted to obtain.
I have written the following PHP function but still get the prompt to download the file:
function navigateToBytes($contentType, $bytes){
header('Content-Type: ' .$contentType);
//header('Content-Transfer-Encoding: binary'); // UPDATE: as pointed out this is not needed, though it does not solve the problem
header('Content-Length: '.strlen($bytes));
ob_clean();
flush();
echo $bytes;
}
An example of calling the function:
navigateToBytes('image/jpeg', $bytes); // UPDATE: turns out this does work, using image/tiff for tiff images is when the browser does not display the image
where $bytes are the bytes as read from the file.
Apologies all - turns out I was having the problem because the images I was testing were TIFF's (with the Content-Type correctly set to image/tiff) when I used a JPEG the browser would display the image!
Ultimately it is up to the browser to decide whether it can display the Content-Type you are sending.
For the record the only headers I needed to change was
Content-Type,
I should set
Content-Length
too unless I set
Transfer-Encoding: chunked
Try the HTTP header "Content-Disposition: Inline", however some browsers may try to save the user from seeing binary data. Here is a random blog article on that HTTP header:
http://dotanything.wordpress.com/2008/05/30/content-disposition-attachment-vs-inline/
That seems like correct behavior to me. The browser is a viewport for humans to view things in. Humans, by and large, don't want to view binary data. What do you think should happen?
Random Advice: If there's a site that's doing what you want to do, use curl to sniff the headers they're sending.
curl -I http://example.com/path/to/binary/file/that/displays/in/browser
and then use the exact same headers in your own script.
As a start, get rid of things that do not exist in HTTP (Content-Transfer-Encoding).
Then get an HTTP tracing tool, such as the Live HTTP headers plugin for Firefox, and compare "your" headers with those received for a working image.
In doubt, post the HTTP trace here.
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