Take this scenario for an example:
- User types “
http://example.com/index.html” into my form - Form is sent to backend script which does
file_get_contents("http://example.com/index.html") - PHP script saves the returned html to a file with the name “
site.html” (file extension based on the extension of the given address)
Now consider this second example:
- User types “
http://example.com” into my form - Form is sent to backend script which does
file_get_contents("http://example.com") - PHP script saves the returned html to a file with the name “
site.com” (file extension based on the extension of the given address)
Clearly this method is not ideal because the file "site.com" is now pretty useless.
My question is, is there a way for PHP to work out what type of file it is getting? In the second example, it could be anything from "index.html" to "default.asp" depending on the server settings.
You can look at the
Content-TypeHTTP header to figure out what type of file you are getting — but you can’t find out what the filename used on the server is (or even if there is a filename), and (in most cases) both index.html and default.asp will return an HTML document.