1

我有这个文件上传输入的形式:

<input type="file"  multiple name="file[]" />

我有一个脚本,它在表单中多次附加它。

我可以确定每个文件 [] 数组在 php 中属于哪一行吗?

<?php 
if(isset($_FILES['file'])=== true){
$files = array ($_FILES['file']);
}
    var_dump($files);

?>

array (size=1)
  0 => 
array (size=5)
  'name' => 
    array (size=5)
      0 => string 'IMG_8502 f 5 .jpg' (length=17)
      1 => string 'IMG_8507 f 5 .jpg' (length=17)
      2 => string 'IMG_8508 f 5 .jpg' (length=17)
      3 => string 'IMG_8529 f 5 .jpg' (length=17)
      4 => string 'IMG_8612 fff 5 .jpg' (length=19)
  'type' => 
    array (size=5)
      0 => string 'image/jpeg' (length=10)
      1 => string 'image/jpeg' (length=10)
      2 => string 'image/jpeg' (length=10)
      3 => string 'image/jpeg' (length=10)
      4 => string 'image/jpeg' (length=10)
  'tmp_name' => 
    array (size=5)
      0 => string 'C:\wamp\tmp\phpE892.tmp' (length=23)
      1 => string 'C:\wamp\tmp\phpE8D1.tmp' (length=23)
      2 => string 'C:\wamp\tmp\phpE911.tmp' (length=23)
      3 => string 'C:\wamp\tmp\phpE950.tmp' (length=23)
      4 => string 'C:\wamp\tmp\phpE99F.tmp' (length=23)
  'error' => 
    array (size=5)
      0 => int 0
      1 => int 0
      2 => int 0
      3 => int 0
      4 => int 0
  'size' => 
    array (size=5)
      0 => int 2776165
      1 => int 2380025
      2 => int 2456713
      3 => int 2585779
      4 => int 1770128

我想将每组文件上传到自己的数组中。换句话说,名称 0-4 设置为数组 0,然后将下一行中上传的下一组文件设置为数组 1,依此类推。所以我可以将每组文件及其相关文本存储在表单中

4

2 回答 2

1
<?php
if(isset($_FILES['userfile'])=== true){
  $files = array ($_FILES['userfile']);
  echo  '<pre>';
  print_r($_FILES['userfile']);
  echo  '</pre>';
}
?>


<form action="" method="post" enctype="multipart/form-data">
  Send these files:<br />
    set one:<br>
  <input name="userfile[0][]" type="file" /><br />
  <input name="userfile[0][]" type="file" /><br />
  set two:<br>
  <input name="userfile[1][]" type="file" /><br />
  <input name="userfile[1][]" type="file" /><br />

  <input type="submit" value="Send files" />
</form>

测试输出:

Array
(
    [name] => Array
        (
            [0] => Array
                (
                    [0] => Chrysanthemum.jpg
                    [1] => Desert.jpg
                    [2] => Hydrangeas.jpg
                    [3] => Jellyfish.jpg
                )

            [1] => Array
                (
                    [0] => Koala.jpg
                    [1] => Lighthouse.jpg
                    [2] => Penguins.jpg
                    [3] => Tulips.jpg
                )

        )

    [type] => Array
        (
            [0] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => image/jpeg
                    [3] => image/jpeg
                )

            [1] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => image/jpeg
                    [3] => image/jpeg
                )

        )

    [tmp_name] => Array
        (
            [0] => Array
                (
                    [0] => C:\xampp\tmp\php4095.tmp
                    [1] => C:\xampp\tmp\php40B5.tmp
                    [2] => C:\xampp\tmp\php40C6.tmp
                    [3] => C:\xampp\tmp\php40C7.tmp
                )

            [1] => Array
                (
                    [0] => C:\xampp\tmp\php40D7.tmp
                    [1] => C:\xampp\tmp\php40D8.tmp
                    [2] => C:\xampp\tmp\php40D9.tmp
                    [3] => C:\xampp\tmp\php40EA.tmp
                )

        )

    [error] => Array
        (
            [0] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )

            [1] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )

        )

    [size] => Array
        (
            [0] => Array
                (
                    [0] => 879394
                    [1] => 845941
                    [2] => 595284
                    [3] => 775702
                )

            [1] => Array
                (
                    [0] => 780831
                    [1] => 561276
                    [2] => 777835
                    [3] => 620888
                )

        )

)
于 2013-01-29T05:31:15.247 回答
0

试试这个:

//This function rearranges the $_FILES array
function rearrangeArray() {
    $newArray = array();

    foreach( $_FILES[ 'file' ][ 'name' ] as $key => $value ) {
        $newArray[ $key ][ 'name' ] = $_FILES[ 'file' ][ 'name' ][ $key ];
        $newArray[ $key ][ 'type' ] = $_FILES[ 'file' ][ 'type' ][ $key ];
        $newArray[ $key ][ 'tmp_name' ] = $_FILES[ 'file' ][ 'tmp_name' ][ $key ];
        $newArray[ $key ][ 'error' ] = $_FILES[ 'file' ][ 'error' ][ $key ];
        $newArray[ $key ][ 'size' ] = $_FILES[ 'file' ][ 'size' ][ $key ];
    }

    $_FILES[ 'file' ] = $newArray;
}

if( isset( $_FILES[ 'file' ] ) ) {
    print_r($_FILES);
    rearrangeArray();
    print_r($_FILES);
}

调用函数之前的 $_FILES

Array
(
    [file] => Array
        (
            [name] => Array
                (
                    [0] => Desert.jpg
                    [1] => Hydrangeas.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => C:\xampp\tmp\phpC349.tmp
                    [1] => C:\xampp\tmp\phpC398.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 845941
                    [1] => 595284
                )

        )

)

调用函数后的 $_FILES

Array
(
    [file] => Array
        (
            [0] => Array
                (
                    [name] => Desert.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\phpC349.tmp
                    [error] => 0
                    [size] => 845941
                )

            [1] => Array
                (
                    [name] => Hydrangeas.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\phpC398.tmp
                    [error] => 0
                    [size] => 595284
                )

        )

)

希望能帮助到你。

于 2013-01-29T05:32:02.547 回答