PHP Upload multiple files only upload 1 file

前端 未结 5 1724
囚心锁ツ
囚心锁ツ 2021-01-15 04:50

i edited this code alot of times (im noob whit php) and my problem is upload multiples files with this code. i can upload only 1 file.

Here

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-15 05:11

    As others have pointed out, you need to change your upload field name to ie. files[] (include the sqaure brackets after the name). This is relevant as it tells PHP that it should treat the field as an array.

    Additionally, in the code, you may use foreach() to access the uploaded files like this:

    foreach ($_FILES['field_name'] as $file)
    

    (obviously, yout html field would be names field_name[] in this case) This will return one array in each of its five iterations, giving you information about all the files you've sent. For instance, if you have sent two files, it may look like this:

        ["name"]=>
        array(2) {
          [0]=>
          string(5) "dir.c"
          [1]=>
          string(10) "errcodes.h"
        }
        ["type"]=>
        array(2) {
          [0]=>
          string(11) "text/x-csrc"
          [1]=>
          string(11) "text/x-chdr"
        }
        ["tmp_name"]=>
        array(2) {
          [0]=>
          string(14) "/tmp/phpP1iz5A"
          [1]=>
          string(14) "/tmp/phpf31fzn"
        }
        ["error"]=>
        array(2) {
          [0]=>
          int(0)
          [1]=>
          int(0)
        }
        ["size"]=>
        array(2) {
          [0]=>
          int(511)
          [1]=>
          int(38)
        }
      }
    

    It's important to understand that PHP will categorize those not into files and then give each its properties, but rather will list the properties for all the files.

    I hope it's clear now.

提交回复
热议问题