0

In my program I allow the user to upload 0 to 10 pics.

But In my php script when I check if a file is upload even if there is no file the script is read.

So even if the user upload just a file ("media-pic1") All the other script are read ( for example if(isset($_FILES['media-pic2'])) is true)

The html:

<input type="file" id="upload-pic1" name="media-pic1" class="hidden">

<input type="file" id="upload-pic2" name="media-pic2" class="hidden">

<input type="file" id="upload-pic3" name="media-pic3" class="hidden">

<input type="file" id="upload-pic4" name="media-pic4" class="hidden">

etc

The php:

if(isset($_FILES['media-pic1']))
{....}

if(isset($_FILES['media-pic2']))
{....}

if(isset($_FILES['media-pic3']))
{....}

...

if(isset($_FILES['media-pic10']))
{....}

PLease help

Dilak
  • 105
  • 2
  • 2
  • 13
  • 2
    [ID's Must Be Unique](http://stackoverflow.com/questions/5611963/can-multiple-different-html-elements-have-the-same-id-if-theyre-different-eleme), specifically because it will cause problems in [JavaScript](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) and CSS when you try to interact with those elements. – Jay Blanchard Jul 11 '17 at 16:49
  • Looks like you'd be better off with a `foreach()` loop. – Jay Blanchard Jul 11 '17 at 16:50
  • Is it related to [this](https://stackoverflow.com/q/17492136/5847906) : [is_uploaded_file](http://php.net/manual/en/function.is-uploaded-file.php) – Ayak973 Jul 11 '17 at 16:54
  • @JayBlanchard the ids were a mistake. I edited the question – Dilak Jul 11 '17 at 16:54
  • @Ayak973 Yes it helped me solve the problem. Thank you very much! – Dilak Jul 11 '17 at 17:01
  • 2
    Possible duplicate of [PHP Upload IF ISSET always says it is?](https://stackoverflow.com/questions/17492136/php-upload-if-isset-always-says-it-is) – Ayak973 Jul 11 '17 at 17:03
  • You should check `$_FILES['media-pic1']['error'] == UPLOAD_ERR_OK` for each files. – Ivan Bolnikh Jul 11 '17 at 17:04

1 Answers1

0

Use for loop for the solution . For displaying html, use following code.

<?php 
    for ($x = 0; $x <= 10; $x++) {
        echo '<input type="file" id="upload-pic'.$x.'" name="media-pic'.$x.'" class="hidden">';

    } 
?>

When uploading in php,

<?php 
    for($x=0;$x<=10;$x++){
        if(is_uploaded_file($_FILES['media-pic'.$x]['tmp_name']){
            //perform required operation.
        }
    }
?>
AmRit GhiMire
  • 43
  • 1
  • 7