Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove extension from file [duplicate]

Possible Duplicate:
How remove extension from string (only real extension!)

I am brand new to php and have a lot to learn! I'm experimenting with MiniGal Nano for our King County Iris Society website. It work quite well for our purposes with one small exception: the photo file name needs to be visible under the thumbnail. I've created a work around, but it shows the extension. I've found code samples of functions but have no idea how to incorporate them into the existing code. Any assistance would be greatly appreciated.

Example link: http://www.kcis.org/kcisphotogallery.php?dir=Iris.Japanese

Many thanks!

like image 251
user1848938 Avatar asked Jan 07 '13 21:01

user1848938


People also ask

How do I remove a double filename extension?

Open File Explorer and click View tab, Options. In Folder Options dialog, move to View tab, untick Hide extensions for known file types option, OK. Then you will se file's extension after its name, remove it.

How do I remove multiple file extensions?

Open any folder window. Press Alt+T+O (that's the letter O, not a zero) to open the Folder Options dialog box. Click the View tab. Remove the tick (checkmark) beside 'Hide extensions for known file types' and click OK.

Does Windows 10 have a duplicate file finder?

Answer: No, Windows 10 does not have a duplicate finder in it yet.


2 Answers

You can use pathinfo() for that.

<?php

// your file
$file = 'image.jpg';

$info = pathinfo($file);

// from PHP 5.2.0 :
$file_name = $info['filename'];

// before PHP 5.2.0 :
// $file_name =  basename($file,'.'.$info['extension']);

echo $file_name; // outputs 'image'

?>
like image 118
hek2mgl Avatar answered Oct 23 '22 03:10

hek2mgl


There are a few ways to do it, but i think one of the quicker ways is the following

// $filename has the file name you have under the picture
$temp = explode('.', $filename);
$ext  = array_pop($temp);
$name = implode('.', $temp);

Another solution is this. I haven't tested it, but it looks like it should work for multiple periods in a filename

$name = substr($filename, 0, (strlen($filename))-(strlen(strrchr($filename, '.'))));

Also:

$info = pathinfo($filename);
$name = $info['filename'];
$ext  = $info['extension'];

// Shorter
$name = pathinfo($file, PATHINFO_FILENAME);

// Or in PHP 5.4
$name = pathinfo($filename)['filename'];

In all of these, $name contains the filename without the extension

like image 43
Ascherer Avatar answered Oct 23 '22 02:10

Ascherer