Hir*_*oki 4 php laravel-5 laravel-5.2
我正在尝试使用 Laravel 5 设置图像上传器。
这是表格。
<form action="/edit/{{$id}}" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="hidden" name="_method" value="PUT">
//There are other tags (including other input) here
<input type="file" name="pic" accept="image/*">
<input type="submit" value="Apply Changes">
</form>
Run Code Online (Sandbox Code Playgroud)
这是将从form上面运行的控制器功能。
public function update($id)
{
$this->model->find($id)->fill($this->request->all())->save();
if ($this->request->hasFile('pic')) {
$file = $this->request->file('pic');
$name = $file->getClientOriginalName();
$file->move('assets/serviceimages/', $name);
}
return redirect('/blahblah');
}
Run Code Online (Sandbox Code Playgroud)
您可能已经注意到,这不会将上传的文件存储在assets/serviceimages/目录下,这正是我一直在尝试的。
基本上我一直在关注本教程,但显然我遗漏了一些东西,并且完全不知道它是什么,即使在我查看了API 文档之后也是如此。
在if控制器函数的声明中,确认了$file和$name符合我的预期。
因此,我怀疑问题出在move()功能上。
任何小的建议将不胜感激,因为这是我第一次尝试设置上传器。
小智 5
如果您使用的是 Linux,则权限。如果这不起作用,请尝试以下方式:
$image = $request->file('pic');
$destinationPathImg = '/your path/images/';
if (!$image->move($destinationPathImg, $image->getClientOriginalName())) {
return 'Error saving the file.';
}
Run Code Online (Sandbox Code Playgroud)
还有这个需要加在ur()
public function update(Request $request, $id)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10153 次 |
| 最近记录: |