How To Get File Extension From Uploaded File
Posted On Mon Jan 31, 2011 By hostman In Wallpaper Scripts Forums And Topics About Programming Forums
like many websites, they offer their visitors to upload files such as images, photos, zip, documents, text, and more, its important to know what type of files you are allowing your visitors to upload to your site. for example, if i have a form for a user to upload an image file, i should have my script check to make sure its an image file and not a file that can harm your server.
so to get the file extension i can use the substr() strrpos() PHP functions to be able to get the type of file a user is uploading..
lets say the file name is called wallpaperama.jpg so this is how i would declare the value of my string:
then i want to find out what are the characters after the . (dot) with this code:
and this is the complete script:
to see the script in action all you have to do is copy and paste the complete code above to a text editor like notepad and save it as wallpaperama.com and upload to your website, then open it with your browser. NOTE: your website must have PHP, if your website doesn't have PHP, go to www.webune.com to sign up for PHP webshosting.
COURTESY: this tutorial guide was brought to you by www.webune.com
so to get the file extension i can use the substr() strrpos() PHP functions to be able to get the type of file a user is uploading..
lets say the file name is called wallpaperama.jpg so this is how i would declare the value of my string:
$fileName = 'wallpaperama.jpg';
then i want to find out what are the characters after the . (dot) with this code:
$ext = substr($fileName, strrpos($fileName, '.') + 1);
and this is the complete script:
<?
$fileName = 'wallpaperama.jpg';
$ext = substr($fileName, strrpos($fileName, '.') + 1);
echo 'The file extension is: .'.$ext;
?>
$fileName = 'wallpaperama.jpg';
$ext = substr($fileName, strrpos($fileName, '.') + 1);
echo 'The file extension is: .'.$ext;
?>
to see the script in action all you have to do is copy and paste the complete code above to a text editor like notepad and save it as wallpaperama.com and upload to your website, then open it with your browser. NOTE: your website must have PHP, if your website doesn't have PHP, go to www.webune.com to sign up for PHP webshosting.
COURTESY: this tutorial guide was brought to you by www.webune.com
rits Tue Jun 23, 2009
hey great..
thnx...
i was really seraching for it
thnx buddy
thnx...
i was really seraching for it
thnx buddy
maca Sun Jul 15, 2007
i would like to learn that, you know, split the file into the filne name and the file extension into two separate string, well, i was able to figure out how to get the file name and remove the extension from the file name with this fucntion:
I use this function to remove the file extension and display only the file name without the extension of the file name: this is good when you have users upload files and you can rename the to whatever you want and then display them on the website with the new name:
OUTPUT:
The file name without extension is: wallpaperama
I use this function to remove the file extension and display only the file name without the extension of the file name: this is good when you have users upload files and you can rename the to whatever you want and then display them on the website with the new name:
<?
function remove_ext($filename) {
$extension = strrchr($filename, '.');
if($extension !== false){
$filename = substr($filename, 0, -strlen($extension));
}
return $filename;
}
$filename = 'wallpaperama.jpg';
echo remove_ext($filename);
?>
function remove_ext($filename) {
$extension = strrchr($filename, '.');
if($extension !== false){
$filename = substr($filename, 0, -strlen($extension));
}
return $filename;
}
$filename = 'wallpaperama.jpg';
echo remove_ext($filename);
?>
OUTPUT:
The file name without extension is: wallpaperama
hostman Sun Jul 15, 2007
that was a good one, and another way is to split the file name and the file extension into an array with this function:
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$filename = 'wallpaperama.jpg';
echo '
';
?>
OUTPUT:
anyone else has a way to display the file name and the extension into an array?
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$filename = 'wallpaperama.jpg';
echo '
'.findexts ($filename).'
';
?>
OUTPUT:
The File Extension is: jpg
anyone else has a way to display the file name and the extension into an array?
webune Sun Jul 15, 2007
another way to do this, i use this php code:
PHP CODE:
$filename = 'wallpaperama.jpg';
preg_match('/\.([A-Za-z]+?)$/', $filename, $matches);
echo "The file exension for $filename is: ".$matches[1]."
and with the dot is ".$matches[0]."";
?>
OUTPUT:
The file exension for wallpaperama.jpg is: jpg
and with the dot is .jpg
courtesy: Webune hosting
PHP CODE:
$filename = 'wallpaperama.jpg';
preg_match('/\.([A-Za-z]+?)$/', $filename, $matches);
echo "The file exension for $filename is: ".$matches[1]."
and with the dot is ".$matches[0]."";
?>
OUTPUT:
The file exension for wallpaperama.jpg is: jpg
and with the dot is .jpg
courtesy: Webune hosting
Related Content
Information
Forums »
Wallpaper Scripts Forums And Topics About Programming »
How To Get File Extension From Uploaded File
Wallpaper Scripts Forums And Topics About Programming »
How To Get File Extension From Uploaded File
Title: How To Get File Extension From Uploaded File
Description: this is a how to display show get file extension getting uploaded file extension type
Tags: php , file , extension , type , upload ,
Info: This Post Has Been Viewed 0 Times Since
Date: Mon Jan 31, 2011
Author hostman Received 4 Replies #1356
Date: Mon Jan 31, 2011
Author hostman Received 4 Replies #1356
Share
URL: 

Embed: 

To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it
BBCODE:: 

BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
wallpaperama | Wallpapers | Forums | Terms Of Service
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5