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


i've html form code select image file , upload server.

i have incomplete code written in php until getting size of image user uploading server.

please have @ code snippets below.

html form code :

<!doctype html> <html> <body>  <form action="upload.php" method="post" enctype="multipart/form-data">     select image 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 actual image or fake image if(isset($_post["submit"])) {     $check = getimagesize($_files["filetoupload"]["tmp_name"]);     echo "check value tmp_name parameter : \n";     echo "<pre>";     print_r($check);      echo "</pre>";     die; } ?> 

output of php code 1 below :

check value 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 actual image or fake image     if(isset($_post["submit"])) {         $check = getimagesize($_files["filetoupload"]["name"]);         echo "check value name parameter : \n";         echo "<pre>";         print_r($check);          echo "</pre>";         die;     }     ?> 

output of php code 2 below :

warning: getimagesize(demo_image.jpg): failed open stream: no such file or directory in c:\xampp\htdocs\php_playground\uploads\upload.php on line 8

check value name parameter :

now, query i'm trying upload same image in both above programs why i'm getting 2 different outputs getimagesize() function changing index value $_files array?

please guide me in regard.

thanks.

because in second code using "name", function getimagesize not able read file.

here's why:

the "name" variable comes browser, hence on server side function unable locate image , hence fails provide information , generate warning. while "tmp_name" temporary path of server side location, upload file temporarily stored. hence when use "temp_name" function able read file , provide information.

for more information, print $_files varibale see difference between "name" , "tmp_name".

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


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -