1

how would I do an if / else statement for such? the user has the option of adding images or not. if the add images, i want the if to run and if they dont I want the else to run because depending on whether or not the add an image they will have two different end.

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

if (isset($_POST['name'])){  }else{  }

if (isset($_POST['state'])){  }else{  }

if (isset($_FILES['images']['name'])) { echo 'images'; exit;} else {echo 'no images'; exit;}


} #end main form submitted

if (isset($_FILES['images']['name'])) dosnt work because as of now even when no images are submitted it still says images submitted.

the html file fields are:

<form>
<input type='file' name='images[]' id=''>
<input type='file' name='images[]' id=''>
<input type='file' name='images[]' id=''>
<input type = "submit">    
</form>
Query Master
  • 6,989
  • 5
  • 35
  • 58
Keezy
  • 293
  • 4
  • 15
  • please post the full PHP code. It is hard to understand your problem with this snippet. – CyprUS Apr 16 '12 at 14:23
  • Take a look at [this](http://stackoverflow.com/questions/946418/how-to-check-if-user-uploaded-a-file-in-php) – jprofitt Apr 16 '12 at 14:30
  • @keezy see my answer below i hope its work for you – Query Master Apr 16 '12 at 14:49
  • @Samad I dont understand how I could use that to do what im tryng to do. when I add this to my code i get: 0 in C:\xampp\htdocs\bx\modules\step_2.php on line 84 – Keezy Apr 16 '12 at 15:06
  • @Keezy its working code and i don't know why this code is problem there. how can i send you a file – Query Master Apr 16 '12 at 15:09
  • @Keezy This code is basically belong to two files one for html.php and second for submit.php see my update answer – Query Master Apr 16 '12 at 15:11
  • @Samad sorry it does work, I was being slow. after I got some sleep and looked at it again i figured it out. thanks buddy =) – Keezy Apr 17 '12 at 05:06

4 Answers4

5

You should try this

Submit.php

<pre>
<?php 
// those index are empty the array filter remove this 

echo "without filter"."<br>";
print_r($_FILES['images']['name']);

echo "filter"."<br>";
$usersFileUpload = array_filter($_FILES['images']['name']);
print_r($usersFileUpload);

$usersFileUploadCount = count($usersFileUpload);

for($i=0;$i<=$usersFileUploadCount;$i++){
    echo $usersFileUpload[$i]."<br>";

    // insert Table     
}
?>

HtmlForm.php

<form action="submit.php" method="post" enctype="multipart/form-data" name="images">
<input type='file' name='images[]' id=''><br />
<input type='file' name='images[]' id=''><br />
<input type='file' name='images[]' id=''>
<input name="" type="submit" />
</form>

See the image for verification 1

enter image description here

See the image for verification 2

enter image description here

Query Master
  • 6,989
  • 5
  • 35
  • 58
0

im not sure i understand, i think you are trying to do this.

if (isset($_FILES['name']) && isset($_FILES['images']))
Jacxel
  • 1,634
  • 8
  • 22
  • 33
0

Try checking to see if $_FILES['images']['error'][$x] is equal to 4. If I recall correctly, that's indicative of an error with the upload.

Obviously, do that for each iteration of your images uploader, so essentially replace $x with the key for the uploader array.

Remember, an empty array is still a set array.

See: http://uk3.php.net/manual/en/features.file-upload.errors.php

Also: http://uk3.php.net/manual/en/features.file-upload.php <-- Manuals. Handy!

David
  • 493
  • 2
  • 6
  • 16
0

Try this

if($_FILES['your-name-file']['error'] == 4) {
            //process your error
    }

It's work for me!

Phi Dinh
  • 1
  • 3