• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

php ecperts... how to parse file extensions?

keith

anti-liberal
NLC
here's basically what i'd like to do, plain and simple:
Code:
$url = "http://somerandomlocation.com/blahblah/file.ext";

if ([b]$url ends with ".jpg"[/b]) {
do function a;
{
elseif ([b]$url ends with ".gif"[/b]) {
do function b;
}
else {
do function c;
}
how would i go about recognizing the file extensions? any pointers?
 
Last edited:
yeah yeah... i misspelled experts. typing too fast with my index fingers for my own good
 
PHP:
url = "http://somerandomlocation.com/blahblah/file.ext";

if ($url = ".jpg$") {
do function a;
}
elseif ($url = ".gif$") {
do function b;
}
else {
do function c;
}
 
PHP:
<?php

$strUrl = 'http://hostname.com/file.php';
$strExtention  = substr($strUrl, -3); 

print 'The file type is ';

if ( $strExtention == 'gif' )
{
	print 'gif';
}
elseif ( $strExtention == 'jpg' )
{
	print 'jpg';
}
else
{
	print 'unknown';
}	

?>
 
hey, thanks people...

bill will, would that work for 4-character extensions as well, such as .jpeg?
 
Here's another method. This time around it just grabs everything after the period in $strFilename.

PHP:
<?php

$strFilename = 'filename.jpg';
list($strBase, $strExtention) = split ('\.', $strFilename);

print 'The file type is ';

if ( $strExtention == 'gif' )
{
    print 'gif';
}
elseif ( $strExtention == 'jpg' )
{
    print 'jpg';
}
elseif ( $strExtention == 'jpeg' )
{
    print 'jpeg';
}
else
{
    print 'unknown';
}

?>
 
why create new variables and use array functions to parse a simple string?

Code:
$url = 'http://somerandomlocation.com/blahblah/file.ext';

switch(substr($url,strrpos($url,'.')+1))
{
case 'gif':
do function a;
break;
case 'jpg':
do function b;
break;
default:
do function c;
}
 
though if what you're trying to do is check what kind of image a file is better than checking the extension would be something like:

Code:
if($x=@getimagesize($url))
{
switch $x[2]
{
case 1:
do function gif;
break;
case 2:
do function jpg;
break;
case 3:
do function png;
break;
case 4:
do function swf;
break;
case 5:
do function pmd;
break;
default:
do function bmp;
}
}
else
{
echo $url.' doesn\'t exist or isn\'t an image';
}
 
quotes in last line need to be escaped of course, damn vbulletin strips things even in code blocks and messes everything up in php blocks...
 
Back
Top