php - 使用isset($ _ FILE)但它总是返回true?

I was wondering that each time i refresh my page do I have to somehow clear $_FILES , or why is it echoing back to me that the variable is set and also NOT empty? when i first load, or reload the page is obviously is at least empty by using print_r

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

<?php 


if(isset($_FILES["file"]) && !empty($_FILES["file"])){
  echo 'You have just uploaded '. $_FILES["file"]["name"];

}
?>

check the form has been posted,else your code while page loading

<?php 

if($_POST['submit']=="submit"){
if(isset($_FILES["file"]) && !empty($_FILES["file"])){
  echo 'You have just uploaded '. $_FILES["file"]["name"];

}
}
?>

action="" means you post the data to the current page, but after you post the data, when you refresh your page, the browser will ask you whether to repost the data, and if you press yes, then the data will be posted again.

The right solution is redirect to the current page after post.

Actually, You are facing this trouble because, after once you submit by selecting a file the browser caches the page response & when you refresh it by F5 or right click->reload it simply returns the cached result. You can try your old code by reloading it by a dummy GET request it will work fine. For example : If the url to this page is :http://localhost/file.php then type in the address bar as : http://localhost/file.php/?dummy=abc then you will find your code works correctly... So, you must verify not just $_FILES but $_FILES["file"]["name"] instead, would go fine...

Try this, you wont have any trouble....

    <html>


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


</html>

<?php 


if(isset($_FILES["file"]) && !empty($_FILES["file"]["name"])){
  echo 'You have just uploaded '. $_FILES["file"]["name"];

}
?>