【问题标题】:File upload $_FILES array is empty文件上传 $_FILES 数组为空
【发布时间】:2015-01-01 13:42:38
【问题描述】:

我想将文件上传到服务器,但$_FILES 数组似乎为空。

我下面的代码有问题吗?

<form action='' method='POST' enctype='multipart/form-data'> 
                <table class='table table-striped table-hover'> 
                    <tr> 
                        <td> 
                            Bestand(en)
                        </td>
                        <td> <input type='file' name='fotos[]' multiple='multiple' /> </td>

                    </tr>
                    <tr> 
                        <td> Aangepaste bestandsnaam </td>
                        <td> <input type='text' name='naam' class='form-control' /> </td>
                    </tr>
                    <tr> 

                        <td> <input type='submit' class='btn btn-primary' value='Fotos toevoegen' name='fotos_toevoegen' /> </td>
                        <td></td>
                    </tr>
                </table>
            </form>

PHP 代码

    if(isset($_POST['fotos_toevoegen'])){

    print_r($_FILES['fotos']['name']);

    $array_lenght = count($_FILES['fotos']['name']);
    //print_r($_FILES['fotos']);
    for($i = 0; $i < $array_lenght; $i++){

        $array_fotos = array();

        // trek $_FILES uit elkaar zodat je individuele foto kan toevoegen.
        foreach($_FILES['fotos'] as $key => $value){
            $array_fotos[$key] = $_FILES['fotos'][$key][$i];
        }



        $foto = new Foto();

        $foto->path = $path . '/cms/fotos/orginele-bestanden';
        $foto->naam = $_POST['naam'].'-'. $i;
        $foto->album_id = $session_album_id;
        $foto->file_info = $array_fotos;
        $foto->width_thumbnail = 300;
        $foto->height_thumbnail = 250;
        $foto->width_grootformaat = 500;
        $foto->height_grootformaat = 400;

        $foto->to_string();

        //$foto_beheer->add($foto);
    }


}

Print_r($_FILES['fotos']) 向我展示了这个: Array ( [name] => Array ( [0] => pannekoeken4.jpg [1] => pannekoeken5.jpg ) [type] => Array ( [0] => [1] => ) [tmp_name] => Array ( [0] => [1] => ) [错误] => 数组 ( [0] => 6 [1] => 6 ) [大小] => 数组 ( [0] => 0 [1] => 0))

【问题讨论】:

  • 你打印像 if(isset($_POST)){echo "
    ";print_r($_FILES);echo "
    ";} 因为你的代码看起来正确跨度>
  • 为什么action是空的?
  • 请添加您的 PHP 代码。
  • @Bhumi Shah 是的 id 做了,我做了 print_r($_FILES) 这就是我发现它是空的。
  • 将操作留空会将您返回到同一页面(我知道这不是一个好习惯)

标签: php file


【解决方案1】:

您能否对照为 post_max_size 配置的值检查上传文件的大小

如果 post 数据的大小大于 post_max_size,则 $_POST 和 $_FILES 超全局变量为空。

请参考以下网址

Click here

【讨论】:

    【解决方案2】:

    一些非常基本的测试给了我以下结果:

    <?php
    if(isset($_POST)){
      echo '<pre>';
      var_dump($_POST);
      var_dump($_FILES);
      echo '</pre>';
    }
    
    
    ?>
    <form action='testfile.php' method='POST' enctype='multipart/form-data'> 
                    <table class='table table-striped table-hover'> 
                        <tr> 
                            <td> 
                                Bestand(en)
                            </td>
                            <td> <input type='file' name='fotos[]' multiple='multiple' /> </td>
    
                        </tr>
                        <tr> 
                            <td> Aangepaste bestandsnaam </td>
                            <td> <input type='text' name='naam' class='form-control' /> </td>
                        </tr>
                        <tr> 
    
                            <td> <input type='submit' class='btn btn-primary' value='Fotos toevoegen' name='fotos_toevoegen' /> </td>
                            <td></td>
                        </tr>
                    </table>
                </form>
    

    输出:

    array(2) {
      ["naam"]=>
      string(17) "customfilename"
      ["fotos_toevoegen"]=>
      string(15) "Fotos toevoegen"
    }
    array(1) {
      ["fotos"]=>
      array(5) {
        ["name"]=>
        array(1) {
          [0]=>
          string(50) "2014-10-06.pdf"
        }
        ["type"]=>
        array(1) {
          [0]=>
          string(15) "application/pdf"
        }
        ["tmp_name"]=>
        array(1) {
          [0]=>
          string(36) "/Applications/MAMP/tmp/php/php79bugX"
        }
        ["error"]=>
        array(1) {
          [0]=>
          int(0)
        }
        ["size"]=>
        array(1) {
          [0]=>
          int(770241)
        }
      }
    }
    

    【讨论】:

    • 是的,但这不是我的服务器上发生的事情,难道不是我的代码而是一些服务器设置?
    • 你检查过这篇文章了吗? stackoverflow.com/questions/3586919/…
    • 我去看看!如果我找到解决方案,我会在这里发布。
    猜你喜欢
    • 1970-01-01
    • 2015-02-04
    • 2021-04-30
    • 2016-10-15
    • 1970-01-01
    • 2021-02-26
    • 2016-03-07
    • 2013-03-21
    • 2019-11-25
    相关资源
    最近更新 更多