7

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?

lorenz
  • 4,538
  • 1
  • 27
  • 45
Mark Gilchrist
  • 1,972
  • 3
  • 24
  • 44

2 Answers2

1

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"]);
  • this has solved the problem, but i want to pass an array of photos to php and then upload them all will this mean that if i pass an array of files it wont iterate thought them? – Mark Gilchrist Mar 30 '15 at 07:04
  • 1
    If your input type file look like this : ``, you certainly have access to an array of files. – Raphaël Gonçalves Mar 30 '15 at 07:50
1

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 :-)