How do you loop through $_FILES array?

前端 未结 8 792
陌清茗
陌清茗 2020-11-28 09:56

Here are the inputs I want to loop through

Main photo:   
Side photo 1: 

        
相关标签:
8条回答
  • 2020-11-28 10:44

    I have struggled with this dilemma for almost a week! Nothing I found on the net could help me. I knew sort of what to do, but could not figure out how to loop through the $_FILES array properly - until now when I read the edited post of the accepted answer.

    I made some changes though, in the script as posted, as it did not work properly for me. I wanted to be able to determine if a file was selected at all, so I changed the line "if( !empty( $_FILES[ 'image' ][ 'error' ][ $index ] ) )" to "if( !empty( $_FILES[ 'image' ][ 'size' ][ $index ] ) )" and then instead of "return false;", I put the size into a variable instead: "$Size = $_FILES[ 'upload' ][ 'size' ][ $index ];"

    This way I could check if the $Size variable was larger than zero. If it was, then a file had been selected and I could continue with counting the number of files and do the actual upload. I did not use any of the "unnecessary" script after "return false;", in the accepted answer. Hope this helps someone.

    : P /MacD

    0 讨论(0)
  • 2020-11-28 10:51

    Maybe:

    Main photo:   <input type="file" name="image1" />
    Side photo 1: <input type="file" name="image2" />
    Side photo 2: <input type="file" name="image3" />
    Side photo 3: <input type="file" name="image4" />
    

    $i=1;
    while (isset($_FILES['image'.$i])) {
        print_r($_FILES['image'.$i]);
        $i++;
    }
    

    If you have to loop through specific file fields.

    0 讨论(0)
提交回复
热议问题