Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing to get file extension when using pathinfo()

Tags:

php

I am building an uploader for a photo site, most of the photos that are uploaded are of type 1232132_1231231_12.jpg. when I run pathinfo() I am getting blank outputs for extension.

Here is my code

$target_dir = "Photos/";
$species =  filter_input(INPUT_POST, 'species');
$country =  filter_input(INPUT_POST, 'country');
$type= filter_input(INPUT_POST, 'type');
include_once 'connection.php';
echo $target_dir . basename($_FILES["file"]["name"]);
for($a=0;$a<count($_FILES['file']['tmp_name']);$a++)
{
$target_file = $target_dir . basename($_FILES["file"]["name"][$a]);
$imageFileType=pathinfo($target_file, PATHINFO_EXTENSION);
var_dump(pathinfo($target_file));

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
    {
       echo "<br> Sorry, only JPG, JPEG, PNG & GIF files are allowed. your file is a  $imageFileType <br>";
    } 
}

Photos/2014-02-21 18.19.08.jpg

 echo $target-file;

gives this output Photos/2014-02-21 18.19.08.jpg

for the var_dump this is echoed out to screen, array(3) { ["dirname"]=> string(6) "Photos" ["basename"]=> string(1) "2" ["filename"]=> string(1) "2" }

Is there something wrong with my code or does pathinfo() not work well with number and special character file names?

Is there another function that does work with these types of file names or should explode() the $target_file with the '.' and take the last element in the returned array?

like image 534
Mark Gilchrist Avatar asked Mar 29 '15 20:03

Mark Gilchrist


People also ask

How can I get file extension in PHP?

The simplest way to get file extension in PHP is to use PHP's built-in function pathinfo. Save this answer.

What does Pathinfo mean in PHP?

The pathinfo() is an inbuilt function which is used to return information about a path using an associative array or a string. The returned array or string contains the following information: Directory name. Basename. Extension.

Which extension is the correct PHP file extension?

php file extension refers to the name of a file with a PHP script or source code that has a ". PHP" extension at the end of it. It's similar to a Word file with a . doc file extension.


2 Answers

The probleme isn't pathinfo. You're doing something weird :

$target_file = $target_dir . basename($_FILES["file"]["name"][$a]);

This line is in a loop where $a is between 0 and... $_FILES["file"]["name"] is returning the filename. but:

$_FILES["file"]["name"][0]

will return the first letter from the filename (2 for your example). So, you're doing a pathinfo on your variable :

$target_file = $target_dir . basename($_FILES["file"]["name"][$a]); // looks like $target_file = Photos/2 (when $a = 0)

Change that line like this :

$target_file = $target_dir . basename($_FILES["file"]["name"]);
like image 54
Raphaël Gonçalves Avatar answered Nov 09 '22 15:11

Raphaël Gonçalves


For me this one worked:

    function convertImageToWebP($source, $destination, $quality=80) {

    if (exif_imagetype($source) == IMAGETYPE_GIF) { $image = imagecreatefromgif($source); }
elseif (exif_imagetype($source) == IMAGETYPE_JPEG) { $image = imagecreatefromjpeg($source); }
elseif (exif_imagetype($source) == IMAGETYPE_PNG) { $image = imagecreatefrompng($source); }

    return imagewebp($image, $destination, $quality);
}

I hope it will solve :-)

like image 23
Umesh Gaire Avatar answered Nov 09 '22 16:11

Umesh Gaire