Stray Dog
Stray Dog

Reputation: 135

Get Image File Extension with PHP

The file name is known but the file extension is unknown. The images in thier folders do have an extension but in the database their names do not.

Example:

$ImagePath = "../images/2015/03/06/";   (Folders are based on date)
$ImageName = "lake-sunset_3";

Does not work - $Ext is empty:

$Ext  = (new SplFileInfo($ImagePath))->getExtension();
echo $Ext;

Does not work either - $Ext is empty:

$Ext  = (new SplFileInfo($ImagePath.$ImageName))->getExtension();
echo $Ext;

Does not work either - $Ext is still empty:

$Ext  = (new SplFileInfo($ImagePath,$ImageName))->getExtension();
echo $Ext;

$Ext should produce ".jpg" or ".jpeg" or ".png" etc.

So my question is simple: What am I doing wrong?

Upvotes: 2

Views: 3801

Answers (5)

maksbd19
maksbd19

Reputation: 3830

Maybe this might help you (another brute and ugly solution)-

$dir = '/path/to/your/dir';
$found = array();
$filename = 'your_desired_file';

$files = scandir($dir);

if( !empty( $files ) ){
    foreach( $files as $file ){
        if( $file == '.' || $file == '..' || $file == '' ){
            continue;
        }

        $info = pathinfo( $file );

        if( $info['filename'] == $filename ){
            $found = $info;
            break;
        }
    }
}

// if file name is matched, $found variable will contain the path, basename, filename and the extension of the file you are looking for

EDIT

If you just want the uri of your image then you need to take care of 2 things. First directory path and directory uri are not the same thing. If you need to work with file then you must use directory path. And to serve static files such as images then you must use directory uri. That means if you need to check files exists or what then you must use /absolute/path/to/your/image and in case of image [site_uri]/path/to/your/image/filename. See the differences? The $found variable form the example above is an array-

$found = array(
    'dirname' => 'path/to/your/file',
    'basename' => 'yourfilename.extension',
    'filename' => 'yourfilename',
    'extension' => 'fileextension'
);

// to retrieve the uri from the path.. if you use a CMS then you don't need to worry about that, just get the uri of that directory.

function path2url( $file, $Protocol='http://' ) {
    return $Protocol.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'], '', $file);
}

$image_url = path2url( $found['dirname'] . $found['basename'] ); // you should get the correct image url at this moment.

Upvotes: 1

Digits
Digits

Reputation: 2684

Now, this is a bit of an ugly solution but it should work. Make sure that all your files have unique names else you'll have several of the same file, which could lead to your program obtaining the wrong one.

<?php
$dir = scandir($imagePath);
$length = strlen($ImageName);
$true_filename = '';
foreach ($dir as $k => $filename) {
    $path = pathinfo($filename);
    if ($ImageName === $path['filename']) {
        break;
    }
}
$Ext = $path['extension'];

?>

Upvotes: 1

leitning
leitning

Reputation: 1506

You're trying to call an incomplete path. You could try Digit's hack of looking through the directory for for a file that matches the name, or you could try looking for the file by adding the extensions to it, ie:

 $basePath = $ImagePath . $ImageName;
 if(file_exists($basePath . '.jpg'))
     $Ext = '.jpg';
 else if(file_exists($basePath . '.gif'))
     $Ext = '.gif';
 else if(file_exists($basePath . 'png'))
     $Ext = '.png';
 else
     $Ext = false;

Ugly hacks aside, the question begging to be asked is why are you storing them without the extensions? It would be easier to strip off the extension if you need to than it is try and find the file without the extension

Upvotes: 0

Jimmy T.
Jimmy T.

Reputation: 4190

getExtension() only returns the extension from the given path, which in your case of course doesn't have one.

In general, this is not possible. What if there is a file lake-sunset_3.jpg and a file lake-sunset_3.png?

The only thing you can do is scan the directory and look for a file with that name but any extension.

Upvotes: 0

nomistic
nomistic

Reputation: 2962

You are calling a file named lake-sunset_3. It has no extension.

SplFileInfo::getExtension() is not designed to do what you are requesting it to do.

From the php site:

Returns a string containing the file extension, or an empty string if the file has no extension.

http://php.net/manual/en/splfileinfo.getextension.php

Instead you can do something like this:

$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);

Upvotes: 0

Related Questions