Question:
PHP: How to get file name without extention?
2007-04-13 08:38:50 UTC
Hi folks,

I have a file uploading tool that I have created. When I upload an image file, I want to be able to generate thumbnail images so I need the filename without the extention. How can I achieve this?

For example. If I upload my_upload_image.jpg, I want ONLY "my_upload_image". This way, I can then show images like "my_upload_image.jpg", "my_image_upload.thumbnail.jpg", etc.

Thanks
Four answers:
2007-04-13 12:01:29 UTC
I'm assuming you are familiar with the image manipulation functions in PHP, so that I don't need to explain how to make thumbnails:



If so, there are a few options. I'd explode it, then get the name by popping off the extension and imploding it:



$fn = $_FILES['myfile']['filename']; // get uploaded file name

$fn = explode(".", $fn); // split into array around periods

$ext = array_pop($fn); // remove last element of array; set aside as extension

$fn = implode(".", $fn); // rejoin file name minus extension

//code to make thumbnail

$tn = "$fn.thumbnail.$ext"; // thumbnail name

$fn = "$fn.$ext"; // file name rejoined with extension



The above assumes all files will come with an extension of some sort; however, if there is no extension, and the file name has a period in it, this will mess up the name.



For example, if I upload a file named:



mypic.isnice.jpg



The code above will set $fn to be:



mypic.isnice



However, if I upload a file named:



mypic.isnice



The code above will set $fn to be:



mypic



A way to work around that is to add a regular expression that checks for one- to four-letter file extensions and executes the code only if there is one:



$fn = $_FILES['myfile']['filename']; // get uploaded file name

if(eregi( '^.*[a-z]{1,4}$', $fn )) {

$fn = explode(".", $fn); // split into array around periods

$ext = array_pop($fn); // remove last element of array; set aside as extension

$fn = implode(".", $fn); // rejoin file name minus extension

//code to make thumbnail

$tn = "$fn.thumbnail.$ext"; // thumbnail name

$fn = "$fn.$ext"; // file name rejoined with extension

}



Or, you could change the regular expression to check for specific file extensions:



$fn = $_FILES['myfile']['filename']; // get uploaded file name

if(eregi( '^.*(.jpg)|(.jpeg)$', $fn)) {

$fn = explode(".", $fn); // split into array around periods

$ext = array_pop($fn); // remove last element of array; set aside as extension

$fn = implode(".", $fn); // rejoin file name minus extension

//code to make thumbnail

$tn = "$fn.thumbnail.$ext"; // thumbnail name

$fn = "$fn.$ext"; // file name rejoined with extension

}
2007-04-13 17:48:31 UTC
With the new release of PHP you can use this:



$newimg = pathinfo("path_to/your_img_name.jpg");



echo $newimg['filename'];



this will show the file name without the extension.



Do this:



$imginfo = pathinfo("my_upload_image.jpg");



echo $imginfo['filename'].".thumbnail".$imginfo['extension'];



This will work. I just did this a week ago.
ecoandy
2007-04-13 15:53:22 UTC

$stuff=substr('your_file.jpg', 1, 8);

echo $stuff;

?>



above, 1 is start character position and 8 is length of string. search web for "php substr" for more. you can also fetch the filename from an array.
Deep Thought
2007-04-13 15:51:33 UTC
use pathinfo



something like



$pinfo = pathinfo("/img/myimage.jpg");

echo $pinfo["basename"]



or basename



basename("/img/myimage.jpg");



both return "myimage"


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...