php - Laravel 5 : the move() function doesn't save the uploaded image file

标签 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>

这是将从 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/

相关文章:

php - MySQL 查询显示 PHP 中的错误

php - 将 mbstring 添加到 laravel 5 应用程序的 docker 图像

php - Laravel 5.2 pdf mime 类型验证问题

php - 如何在 Laravel 5.2 中使用不同的数据库表列名登录?

php - Laravel 5.2 |上传文件 - 在 null 上调用成员函数 getClientOriginalName()

php - 如何在 PHP 中对文件进行数字签名

php - 智能识别不同账户为同一用户

php - 将经常访问的数据存储在文件中而不是 MySQL

php - 带有动态参数的 Laravel 5 全局作用域

php - 请求-> 除了在laravel 5 中给我一个数组而不是一个请求对象