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?
The simplest way to get file extension in PHP is to use PHP's built-in function pathinfo. Save this answer.
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.
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.
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"]);
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 :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With