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

7
  • 2
    ID's Must Be Unique, specifically because it will cause problems in JavaScript and CSS when you try to interact with those elements. Commented Jul 11, 2017 at 16:49
  • Looks like you'd be better off with a foreach() loop. Commented Jul 11, 2017 at 16:50
  • Is it related to this : is_uploaded_file Commented Jul 11, 2017 at 16:54
  • @JayBlanchard the ids were a mistake. I edited the question Commented Jul 11, 2017 at 16:54
  • 2
    Possible duplicate of PHP Upload IF ISSET always says it is? Commented Jul 11, 2017 at 17:03

1 Answer 1

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.
        }
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.