0

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"];

}
?>
3
  • Not a solution to your problem, but you can use only !empty() since empty does not generate an error if checked variable is not set.
    – user1299518
    Commented Feb 20, 2014 at 5:49
  • use header() after post Commented Feb 20, 2014 at 5:57
  • Simply if ($_FILES) { ... } should work just fine.
    – Ja͢ck
    Commented Feb 20, 2014 at 6:04

4 Answers 4

2

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"];

}
?>
1

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"];

}
}
?>
1

The Answer by @Karthick Kumar is wrong because when there is an <input type="file" name="file">, even if no file selected by user, the $_FILES["file"] would never be empty (use print_r() and see it for yourself)!

@Anirban's answer is completely true, I'm just gonna explain it in a more convincing way:

echo( !empty($_FILES["file"]["name"]) ); //works as expected
echo( !empty($_FILES["file"]["type"]) ); //works as expected
echo( !empty($_FILES["file"]["tmp_name"]) ); //works as expected

echo( $_FILES["file"]["size"] > 0 ); //more convincing way!

And the best way would be:

echo( $_FILES["file"]["error"] == 0 ); //you can check the cause of error this way!

Link to the list of error codes

0

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.

1
  • can you elaborate on how to redirect after i post the data? Commented Feb 20, 2014 at 5:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.