0
<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>

Above is my HTML code. Following is my upload PHP code:

<?php
$target_dir = "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"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

When I upload an image it returns the following:

Warning: getimagesize(): Filename cannot be empty in C:\wamp64\www\company\test\upload.php on line 8

File is not an image.

Is there anything wrong with the code? I am using PHP v5.6.16, please help me.

$_FILES["fileToUpload"]["tmp_name"]);

is empty but,

$_FILES["fileToUpload"]["name"]);

is not empty.

  • 2
    Possible duplicate of [Warning: getimagesize() \[function.getimagesize\]: Filename cannot be empty warning message?](http://stackoverflow.com/questions/16043892/warning-getimagesize-function-getimagesize-filename-cannot-be-empty-warnin) – Ravi Hirani Mar 10 '17 at 08:57
  • I looked into that question. but the thing is $_FILES['fileToUpload']['tmp_name'] is empty. but $_FILES['fileToUpload']['name'] is not empty. why is that?? – Chinthaka Udayasiri Mar 10 '17 at 09:09

2 Answers2

0
if(isset($_POST["submit"]) && isset($_FILES['fileToUpload'])) {
    $file_temp = $_FILES['fileToUpload']['tmp_name'];   
    $info = getimagesize($file_temp);
    if($info !== false) {
       echo "File is an image - " . $info["mime"] . ".";
       $uploadOk = 1;
    } else {
       echo "File is not an image.";
       $uploadOk = 0;
    }
} 
else if(isset($_POST["submit"]) && !isset($_FILES['fileToUpload'])) {
    print "Form was submitted but file wasn't send";
    exit;
}
else {
    print "Form wasn't submitted!";
    exit;
}
Naincy
  • 2,953
  • 1
  • 12
  • 21
  • I did according to your answer.. the thing is $_FILES['fileToUpload']['tmp_name'] is empty, but $_FILES['fileToUpload']['name'] not empty. why is that?? – Chinthaka Udayasiri Mar 10 '17 at 09:12
0

You need to check $_FILES is not empty using !empty

if(isset($_POST["submit"]) && !empty($_FILES["fileToUpload"]) {
     // do your stuff
}

OR just check key exist and not null using isset

if (isset($_POST['submit'], $_FILES['fileToUpload'])) {
    // do your stuff
}
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • I did according to your answer.. the thing is $_FILES['fileToUpload']['tmp_name'] is empty, but $_FILES['fileToUpload']['name'] not empty. why is that?? – Chinthaka Udayasiri Mar 10 '17 at 09:11
  • Check this links:- http://stackoverflow.com/questions/14472741/files-field-tmp-name-has-no-value-on-jpg-file-extension and http://stackoverflow.com/questions/30358737/php-file-upload-error-tmp-name-is-empty – Ravi Hirani Mar 10 '17 at 09:16