Search code examples
phpfile-uploadimage-uploadingsuperglobalsgetimagesize

Why the getimagesize() function is behaving differently for different parameter[index from $_FILES super-global array] passed?


I've an HTML form with the code to select an image file and upload it to the server.

I also have an incomplete code written in PHP until getting the size of the image that user is uploading to the server.

Please have a look at the code snippets below.

HTML form code :

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

PHP code 1 :

<?php
$target_dir = "C:/xampp/htdocs/php_playground/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    echo "Check Value with tmp_name parameter : \n";
    echo "<pre>";
    print_r($check); 
    echo "</pre>";
    die;
}
?>

Output of PHP code 1 is as below :

Check Value with tmp_name parameter :

Array
(
    [0] => 1536
    [1] => 2048
    [2] => 2
    [3] => width="1536" height="2048"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)

PHP code 2 :

 <?php
    $target_dir = "C:/xampp/htdocs/php_playground/uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["name"]);
        echo "Check Value with name parameter : \n";
        echo "<pre>";
        print_r($check); 
        echo "</pre>";
        die;
    }
    ?>

Output of PHP code 2 is as below :

Warning: getimagesize(demo_image.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\php_playground\uploads\upload.php on line 8

Check Value with name parameter :

Now, my query is as I'm trying to upload the same image in both the above programs why I'm getting two different outputs for the getimagesize() function for changing the index value from $_FILES array?

Please guide me in this regard.

Thanks.


Solution

  • Because in your second code where you are using "name", the function getimagesize is not able to read the file.

    Here's why:

    The "name" variable comes from your browser, hence on the server side the function is unable to locate the image and hence fails to provide information and generate a warning. While the "tmp_name" is a temporary path of your server side location, where the upload file is temporarily stored. Hence when you use "temp_name" the function is able to read the file and provide you information.

    For more information, just print your $_FILES varibale to see the difference between "name" and "tmp_name".

    Ref: http://php.net/manual/en/reserved.variables.files.php