gpt4 book ai didi

php - Laravel 上传文件 Mime 类型错误

转载 作者:行者123 更新时间:2023-12-02 21:18:37 25 4
gpt4 key购买 nike

我在下面的代码中遇到了一些错误,我搜索了很多,但没有得到解决方案。我想上传file.xls,我错了吗?谢谢

我的 HTML 表单:

 <form class="form-horizontal" role="form" method="post" action="{{ URL::to('doc/test') }}" enctype="multipart/form-data">
<input style='width:400px;' type='file' id='files' name='files[]' class="form-control" multiple='multiple'>
<button type="submit" class="btn btn-primary">Save</button>
</form>

我的 Controller 功能

public function uploadDocs()
{
$files = Input::file('files');
$errors = "";
foreach($files as $file)
{
$rules = array('file' => 'mimes:png,gif,jpeg,pdf,doc,docx,xls,xlsx,max:1000000'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes())
{
$destinationPath = 'documents';
$filename = $file->getClientOriginalName();
$temp = explode(".", $filename);
$extension = end($temp);
$filename = $temp[0].'_'.date('Y-m-d H:i:s').$extension;
$upload_success = $file->move($destinationPath, $filename);
}
else
{
return Redirect::back()->withErrors($validator);
}
}

}

错误:

The file must be a file of type: png, gif, jpeg, pdf, doc, docx, xls, xlsx, max:1000000.

最佳答案

我在通过 Laravel 验证器进行 mime 类型验证时也遇到了问题,所以我最终用这样的方法手动完成了

$supported_mime_types = array('application/vnd.ms-excel');
$file = Input::file('file');
$mime = $file->getMimeType();

if (!in_array($mime, $supported_mime_types)) {
// mime type not validated
}

您仍然可以保留文件大小的规则

关于php - Laravel 上传文件 Mime 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29071656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com