PDA

View Full Version : simple test for if file has been selected


maxelcat
July 30th, 2007, 23:27
:?

Dear ALl

I have what I think is a very simple piece of code.

<?php

if (isset($_POST['submitted'])){

if(!isset($_FILES['upload'])){
echo '<br />a file was selected';
}else{
echo '<br />a file was not selected';
}



}//end of submitted

?>


<form enctype="multipart/form-data" action="upload_test.php" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="16000000" />
<fieldset><legend>Select a File</legend>
<input type="file" name="upload" />
</fieldset>
<input type="submit" name="submit" value="submit me" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
:


I want a simple test for if the user has picked a file or not. I figured that if(isset$_files['upload']){....}else{...} might be a good one but it doesn't work.

If you have no file selected and press submit then you get "No file selected". If you then select a file and press submit you sill get no file selected. Worse, if you then re-load the page, select a file and press submit you STILL get "No file selected"

I expect I am doing something stupid here...

can someone please advise!

Thanks

Edward

rich w
July 30th, 2007, 23:55
What about..

if(strlen($_files["upload"])>0)

maxelcat
July 31st, 2007, 21:19
Thanks for that sadly it doesn't work - same errors as before...

however, i did get this to work:

if (is_array($_FILES['upload']) && $_FILES['upload']['error'] == 0)

whoich seems to work just fine.

Thanks for your time!

Tyler
July 31st, 2007, 21:49
I know you got it, but just in case someone else ends up searching for something else, there's a few different ways to do it. One of them is checking for size as well.


if($_FILES['upload']['size'] > 0)


And there are a few more that I can't think of off the top of my head.

But glad you got it working Edward. :)