Parse a link: relative, absolute or url to find the extension
I have this code
$ext = strtolower( pathin开发者_如何学Gofo($source, PATHINFO_EXTENSION) );
Then I have a bunch of if
statements that work out what code to run based on the extension. Now here's my complicated part. $source can be either a local path "media/images/me.png", or any other file (like mp3's jpg's etc), OR it can be a link, like
http://www.google.com.au/images/nav_logo36.png
My pathinfo parses the above thing correctly. It fails when I add something to the end like a query string
http://www.google.com.au/images/nav_logo36.png?me=1
returns png?me=1
or
http://www.google.com.au/images/nav_logo36.png&s=3
returns png&s=3
or even worse
http://www.google.com.au/images/nav_logo36.png?j=p3nd&new_ext=.png
returns png
or possibly the worse
http://www.google.com.au/images/nav_logo36.png&j=p3nd&new_ext=.png
(the & instead of the ?)
What's the best way to handle this? I'm thinking of stripping out the query string before I start, but am unsure how.
thanks.
LAMP / PHP 5.2
EDIT
Requirements: I need the extension of the file.
It depends on what you need. If the information you retrieve are anywhere in the URL, you could have a look at the function parse_url()
. According to the php manual:
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
The above example will output:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
/path
and this gives you each url part, so you can work to the one you need.
Of course by using $_SERVER['QUERY_STRING'] you just get the query and work on that, or strip it off the full url to get the base bath. Since it seems you say URL sometimes are not well formed (using & instead of ? for the first paramater) using explode('?',$url) may lead to unpredicted results.
This can be done fairly reliably using the cURL library, and curl_getinfo()
See: http://www.php.net/manual/en/function.curl-getinfo.php
The only issue is this will actually perform the request, so it will take the time to receive the response (could be long for large images).
So its a choice between trying to use the file extension that will get you an unreliable result because:
- A file with .png on the end isn't necessarily a png image
- A URL without an extension can quite easily be an image (they can be served however)
And having to receive the response to check the mimetype.
Here is some example code if you want to try it out. This fetches google's logo on the Australian website, and echos the mimetype received in the header.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com.au/images/logos/ps_logo2.png');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
if(!curl_errno($ch)) {
$mimetype = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
echo $mimetype;
}
else {
die(curl_error($ch));
}
curl_close($ch);
You could take it a step further and actually download the file and save it somewhere to use the Fileinfo extension on it.
file_put_contents('tmp',
file_get_contents('http://www.google.com.au/images/logos/ps_logo2.png')
);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, 'tmp');
finfo_close($finfo);
HTTP headers could be sent for files that's aren't of that type. Fileinfo should give a very reliable result, because it tries to guess the mimetype based on certain bytes in the actual file.
精彩评论