1

私はこのファイルアップロード入力をフォームに持っています:

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

これをフォーム内に複数回追加するスクリプトがあります。

PHPで各file[]配列がどの行に属しているかを判断できますか?

<?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 に答える