【问题标题】:Laravel 4.x :Validation input file doesn't workLaravel 4.x:验证输入文件不起作用
【发布时间】:2014-05-21 03:24:14
【问题描述】:

大家好:我这里有个问题!我正在使用 laravel 框架做我的项目。当我尝试验证输入文件时遇到问题。我做了一个简单的网站来测试同样的问题。这是我的路线:

Route::get('test','TestController@uploadFile');

Route::post('test','TestController@uploadFile');

这是我的控制器:

class TestController extends Controller{
public function uploadFile(){
    return View::make('test');
}

public function pUploadFile(){
    try {
        $file=Input::file('file');
        $validator=Validator::make(array('file' => $file ),array('file' =>'image|mimes:jpeg,jpg,png,gif|max:3072'));
        if($validator->fails()){
            return View::make('test')->withErrors($validator);
        }
        $destinationPath = "uploads";
        $extension =$file->getClientOriginalExtension();
        $filename='testfile'.$extension;
        $upload_success = $file->move($destinationPath, $filename);
        if($upload_success)
            return 'succeeded';
        return 'failed';
    } catch (Exception $e) {
        return $e;
    }
}
}

这是我的 Blade.php 文件:

<!DOCTYPE HTML>

<html> 
    <head>
        <meta charset="UTF-8">
</head>  
<body>
    <div class="container">
        <form method="post" action="{{Asset('test')}}" id="form-register"   enctype="multipart/form-data">     
            <h2>Choose an image</h2>
            <input type="file" name="file" id="file"/>
            {{$errors->first('file')}}
            <button>Upload</btn>
        </form>
    </div>
</body>

</html>

当我选择一个大小为:66mb 的 swf 文件并按下按钮时,我得到了一个错误:

警告:64859333 字节的 POST 内容长度超过了 Unknown on line 0 中 8388608 字节的限制

它不应该跨越验证规则!这是为什么?任何人都可以帮助我吗? ps:对不起,如果我的英语很糟糕:D

非常感谢大家。有用!!!但现在新的问题来了。当人们选择比“post_max_size”更大的文件时,我们无法控制他们。哈哈

【问题讨论】:

    标签: php validation file-upload laravel-4


    【解决方案1】:

    它不是验证规则问题,它与您在那里的设置有关。

    确保将 php.ini 中的 post_max_size 和 max_file_uploads 更新为更大的值。

    max_file_uploads = 256 post_max_size=256

    您可以使用 phpinfo() 函数找到您的 php.ini 文件在哪里。

    http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize

    post_max_size integer

    设置允许发布数据的最大大小。这个设置也 影响文件上传。要上传大文件,此值必须更大 比upload_max_filesize。如果您的配置启用了内存限制 脚本,memory_limit 也会影响文件上传。通常来说,一般来说, memory_limit 应该大于 post_max_size。当一个整数是 使用时,该值以字节为单位。速记符号,如所述 在这个FAQ中,也可以使用。如果发布数据的大小更大 与 post_max_size 相比,$_POST 和 $_FILES 超全局变量为空。 这可以通过各种方式进行跟踪,例如通过传递 $_GET 处理数据的脚本的变量,即 ,然后检查是否 $_GET['processed'] 已设置。

     max_file_uploads integer
    

    允许同时上传的最大文件数。 从 PHP 5.3.4 开始,提交时留空的上传字段不会 计入此限制。

    【讨论】:

      【解决方案2】:

      您的 PHP 配置最多只能上传 8MB。调整 php.ini 中的 post_max_sizeupload_max_filesize

      upload_max_filesize = 128M
      post_max_size = 128M
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-09
        • 2013-04-17
        • 1970-01-01
        • 1970-01-01
        • 2018-01-10
        • 1970-01-01
        • 2017-10-23
        相关资源
        最近更新 更多