简体   繁体   中英

Laravel 4.x :Validation input file doesn't work

Hey everyones: I've got a problem here! I'm doing my project using laravel framework. I've got a problem when I try to validate an input files. I made a simple site for testing with a same problem. Here's my Route:

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

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

Here's my controller:

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;
    }
}
}

And here's my blade.php file :

<!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>

When I chose a swf file with size:66mb and push the button, I got an error:

Warning: POST Content-Length of 64859333 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

It shouldn't cross over the validation rules! Why is that? Can anyboby help me? ps:Sorry if my english is terrible :D

Thank you very much all you guys. It works!!! But now the new problem has coming. We can't control people when they choose a large file than 'post_max_size'. lol

Your PHP configuration only allows for a maximum of 8MB to be uploaded. Adjust the post_max_size and upload_max_filesize in your php.ini.

upload_max_filesize = 128M
post_max_size = 128M

Its not validation rules problem its related to settings you have in there.

Make sure you update post_max_size and max_file_uploads in php.ini to a larger value.

max_file_uploads = 256 post_max_size= 256

You can find where is your php.ini file with phpinfo() function.

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

post_max_size integer

Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. If memory limit is enabled by your configure script, memory_limit also affects file uploading. Generally speaking, memory_limit should be larger than post_max_size. When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used. If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. This can be tracked in various ways, eg by passing the $_GET variable to the script processing the data, ie , and then checking if $_GET['processed'] is set.

 max_file_uploads integer

The maximum number of files allowed to be uploaded simultaneously. Starting with PHP 5.3.4, upload fields left blank on submission do not count towards this limit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM