我正在向Laravel提交一个文件字段数组,我想对每个文件运行验证以检查它是否是正确的mime类型(应该只允许PNG,JPG和GIF)但是如果我上传了一个TIF图像文件验证通过。以下是我提交的内容:
Request URL:http://local.website.com/upload
Request Method:POST
Status Code:200 OK
Remote Address:127.0.0.1:80
Response Headers
view source
Connection:Keep-Alive
Content-Length:6
Content-Type:text/html; charset=UTF-8
Date:Mon, 02 May 2016 19:05:18 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3
X-Powered-By:PHP/5.6.3
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:19912006
Content-Type:multipart/form-data; boundary=----WebKitFormBoundarypWGUvcecHBZlzNcP
Cookie:__utma=252126427.1839499577.1452033482.1452033483.1452072866.2; __utmz=252126427.1452033483.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _gat=1; _ga=GA1.2.1839499577.1452033482; laravel_session=eyJpdiI6InVxazdLSFwvNWNRUnl2eDQrRmFhdUpBPT0iLCJ2YWx1ZSI6IlJRXC9mRk9EYXBUWUpQMHpFMms3Mm9hYkRiekNjUlJHTDlBS0hpYkk2R091RmFYQzVsM1JWcEdMdmFzRjl3Q3BoaEZHQUM3VFc5ZE5Oak5KdHowaU0zdz09IiwibWFjIjoiZTYxM2U3MmVhNzU4NmFiZDNmYWFhNjdiZDE3NDczM2IxN2RmYWFhYWZlMzhiNTBiM2IwZjEyYWQwNThhMTk1MyJ9
Host:local.website.com
Origin:http://local.website.com
Pragma:no-cache
Referer:http://local.website.com/
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
------WebKitFormBoundarypWGUvcecHBZlzNcP
Content-Disposition: form-data; name="files[]"; filename="eye design.png"
Content-Type: image/png
------WebKitFormBoundarypWGUvcecHBZlzNcP
Content-Disposition: form-data; name="files[]"; filename="hd flamingo.tif"
Content-Type: image/tiff
------WebKitFormBoundarypWGUvcecHBZlzNcP
Content-Disposition: form-data; name="files[]"; filename="Picture 3.tif"
Content-Type: image/tiff
------WebKitFormBoundarypWGUvcecHBZlzNcP
Content-Disposition: form-data; name="files[]"; filename="Picture 16.tif"
Content-Type: image/tiff
------WebKitFormBoundarypWGUvcecHBZlzNcP
Content-Disposition: form-data; name="files[]"; filename="Picture 23.tif"
Content-Type: image/tiff
------WebKitFormBoundarypWGUvcecHBZlzNcP--
以下是我的要求的内容:
<?php namespace App\Http\Requests;
use Response;
use Illuminate\Foundation\Http\FormRequest;
class UploadRequest extends FormRequest
{
public function rules()
{
$rules = [
'files' => 'required'
];
foreach($this->files as $key => $file) {
$rules['files.'.$key] = 'image|mimes:jpeg,png,gif';
}
return $rules;
}
public function messages()
{
$messages = [
'files.required' => 'You must upload a file.'
];
foreach($this->files as $key => $file) {
$messages['files.'.$key.'.image'] = 'The upload file must be an image.';
$messages['files.'.$key.'.mimes'] = 'The image must be one of the following types: JPEG, PNG, or GIF.';
}
return $messages;
}
public function authorize()
{
return true;
}
}
这是我提交表单的路线:
Route::post('upload', ['uses' => 'UploadController@index', 'as' => 'upload.post']);
这是我的UploadController文件:
<?php namespace App\Http\Controllers;
use Response;
use Illuminate\Http\Request;
use Input;
use Image;
use Session;
use App\Http\Requests\UploadRequest;
use Auth;
use App\Setting;
use App\Discount;
use App\User_credit;
use App\Customer_group_discount;
use Event;
use App\Events\ImagesUpdated;
class UploadController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Handles the uploading of images
*
* @return Response json
*/
public function index(UploadRequest $request)
{
// validation has passed do stuff
}
}
请求肯定在运行,但它应该没有失败。我做错了什么?
答案 0 :(得分:2)
我最终解决了,现在是我的要求:
2048
答案 1 :(得分:0)
使用自定义请求执行验证时,由于某些原因,Laravel不包含上载的文件。解决此问题的第一种方法是使用手动验证器,如Laravel 4中那样。
另一种方法是在请求中包含上传的文件。在扩展请求类中,添加以下内容:
// 'massage' data so that files get in as well
protected function getValidatorInstance()
{
$data = $this->all();
$data['logo'] = $this->file('project_logo');
$data['banner'] = $this->file('project_banner');
$this->getInputSource()->replace($data);
return parent::getValidatorInstance();
}
代码的作用是覆盖请求的getValidatorInstance()
方法,并手动将文件放入其中。在这种情况下,我需要验证两个文件,并且HTML表单中的名称分别为project_logo
和project_banner
。
答案 2 :(得分:0)
这不是确切的解决方案,而是一种简单的检查文件的技巧。
if($request->file('docs')) {
$extensions_allowed = ['pdf', 'docx'];
foreach ($request->file('docs') as $file) {
if(in_array($file->getClientOriginalExtension('docs'), $extensions_allowed)){
return "yes----";
}else {
return "No-----";
}
}
}