我正在尝试使用 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>
这是将从
form
运行的 Controller 功能以上。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');
}
您可能已经注意到,这不会将上传的文件存储在
assets/serviceimages/
下。目录,这是我一直在尝试的。基本上我一直在关注 this tutorial ,但显然我遗漏了一些东西并且完全不知道它是什么,即使在我查看了 API documentation 之后也是如此。 .
内部
if
声明 Controller 功能,确认$file
和 $name
达到了我的预期。因此,我怀疑问题出在
move()
功能。任何小的建议将不胜感激,因为这是我第一次尝试设置上传器。
最佳答案
如果您使用的是 Linux,则权限。如果这不起作用,请尝试以下方式:
$image = $request->file('pic');
$destinationPathImg = '/your path/images/';
if (!$image->move($destinationPathImg, $image->getClientOriginalName())) {
return 'Error saving the file.';
}
还有这个需要加在ur()
public function update(Request $request, $id)
关于php - Laravel 5 : the move() function doesn't save the uploaded image file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36469940/