【发布时间】:2018-05-29 08:10:44
【问题描述】:
这是我的 HTML:
<label for="attachement1">Attach a file: <small style="color:#999;">(type: zip/rar and below 10mb)</small></label>
<input type="file" name="file1"/><br/>
<label for="snapshot">Snapshot / Thumbnail:</label>
<input type="file" name="thumbnail" required/><br/>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-primary" name="Submit" value="Publish" />
这是我的控制器文件中的代码(用于更新功能):
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'thumbnail' => 'mimes:jpg,jpeg,png|max:800',
'file1' => 'mimes:rar,zip|max:10000',
]);
$file1=$request->file('file1');
if(is_null($request->file('file1'))){
$p=pages::where('id', '=', $request['id'])->first();
$attmt1=$p->attachment;
}
else
{
$upload_dir='uploads';
$attmt1=$file1->getClientOriginalName();
$move=$file1->move($upload_dir, $attmt1);
}
if(is_null($request->file('thumbnail'))){
$p=pages::where('id', '=', $request['id'])->first();
$image=$p->thumbnail;
}
else
{
$img=$request->file('thumbnail');
$upload_dir='thumbnails';
$image=$img->getClientOriginalName();
$move=$img->move($upload_dir, $image);
//end thumbnail process
}
$mypage->title = $request->title;
$mypage->body = $request->body;
//$mypage->thumbnail = $request->thumbnail;
$mypage->slug = str_slug($request->slug, '-');
$mypage->menu_name = $request->menu_name;
$mypage->save();
return redirect()->route('menupages.index')->with('message', 'Page updated successfully.');
}
当我尝试编辑项目并上传图像(.jpg 格式)并单击提交时,我收到“缩略图必须是以下类型的文件:jpg、jpeg、png。”我检查了数据库,没有记录文件。
由于某种原因,它会将图像检测为某种外来图像文件类型,即使它是 .jpg。
【问题讨论】:
-
回归基础,你在form标签中实现enctype = multipart/form-data了吗??
-
是的,这是代码的开头:
<form class="form-horizontal" role="form" method="post" action="savepost" enctype="multipart/form-data"> -
尝试 var_dump() 检查文件类型和发布的请求
-
错误:“var_dump() 需要至少 1 个参数,给定 0”我使用了
<?php echo var_dump(); ?> -
再次将文件另存为 jpg 并重试。该文件可能是 GIF 并手动重命名为 JPG