PHP test if form has file uploaded [closed]

2019-09-20 12:58发布

问题:

How do I do it? These don't work:

if(empty($_FILES)){
  echo "there is no file uploaded";
  exit;
} else {
  echo "there is file";
}

if(sizeof($_FILES)!=0){
  echo "there is file";
} else {
  echo "there is no file";
}

回答1:

Try

Sample :

<?php

if (isset ( $_FILES ['image'] )) {
    if ($_FILES ["image"] ["error"] == UPLOAD_ERR_OK) {
        echo "File Was Uploaded AND OK";
    } else {
        echo "File Was Uploaded but BAD";
    }
} else {
    echo "No File Uploaded";
}

?>

<form method="post" enctype="multipart/form-data">
    <label for="file">Filename 1:</label> <input type="file" name="image"
        id="file" /> <br /> <input type="submit" name="submit" value="Submit" />
</form>

You can see more examples here http://php.net/manual/en/function.move-uploaded-file.php



回答2:

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

"If $_FILES is empty ... try adding enctype="multipart/form-data" to the form tag and make sure you have file uploads turned on."



回答3:

you can use is_array($_FILES) to check if it contains files info in array format or not



标签: php forms