【问题标题】:I can not upload images to the public path in Shared Server Laravel 5.5我无法将图像上传到共享服务器 Laravel 5.5 中的公共路径
【发布时间】:2018-01-18 23:25:46
【问题描述】:

我有一个项目托管在 Blue Host 中的共享服务器上,并且在 AdminPostController 的存储和更新方法中我有一个将图像保存在 public_path 中的功能,但是在尝试保存它时会产生以下错误:

Intervention\Image\Exception\NotWritableException
Can't write image data to path (/home4/directoryname/public/directoryname/uploads/posts/1516313973.jpg)

这是我的存储方法:

public function store(Request $request)
{
    $posts = new Post();
    $posts->title           = $request->input('title');
    $posts->slug            = str_slug($request->get('title'));
    $posts->content         = $request->input('content');
    $posts->category_id     = $request->input('category_id');
    $posts->user_id         = $request->input('user_id');
    $posts->blockquote      = $request->input('blockquote');
    $posts->blockquote_sign = $request->input('blockquote_sign');
    $posts->save();

    // Handle the client upload image id avatar
    if($request->hasFile('avatar')){
        $avatar = $request->file('avatar');
        $filename = time() . '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(500, 300)->save( public_path('uploads/posts/' . $filename ) );

        $posts->avatar = $filename;
        $posts->save();
    }

    return redirect()->route('contenido.index')
                    ->with('success','Publicación creada correctamente!!!');
}

这是我的更新方法:

public function update(Request $request, $id)
{
    $updated = Post::findorFail($id);

    $post = $request->all();

    $updated->fill($post)->save();

    if($request->hasFile('avatar')){
        $avatar = $request->file('avatar');
        $filename = time() . '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(500, 300)->save( public_path('uploads/posts/' . $filename ) );

        $updated->avatar = $filename;
        $updated->save();
    }

    return redirect()->route('contenido.index')
                    ->with('success','Publicación actualizada correctamente!!!');
}

这是这些方法的路线资源:

Route::resource('contenido', 'Admin\AdminPostController');

我正在阅读不同的地方,目前我还没有找到解决方案,有人可以指导我吗?。

这就是服务器上文件的结构。

【问题讨论】:

  • 您是否检查过您要写入的路径的读写权限?此外,您不应将用户上传的内容存储在公共目录中,而是创建指向存储的符号链接。
  • 晚上好,感谢您的回答。是的,我已授权 775 和 777 进行测试,但错误仍然存​​在。
  • Laravel 有一个辅助方法来存储上传,你可以试试。在此处查看文档:laravel.com/docs/5.5/filesystem#file-uploads
  • 我尝试使用 storage_path 方法,它对我有用,但它会将文件发送到一个名为 storage 的目录,Laravel 在该目录中注册日志和其他框架功能。
  • 是的,这是存储文件的正确位置。您要做的是从 public/ 中创建一个符号链接到 storage/ 中的上传目录

标签: php laravel laravel-5.4 laravel-5.5 bluehost


【解决方案1】:

Laravel 5 默认使用公用文件夹作为主目录,而我的服务器使用另一个。因此,我设置了一个备用主目录。

我只需要执行以下操作:

1 - 编辑位于 Laravel 5 项目公共文件夹中的 index.php。

2 - 找到以下行:

$app = require_once __DIR__.'/../bootstrap/app.php';

3 - 在该行下方添加以下行:

$app->bind('path.public', function() {
           return __DIR__;
});

4 - 现在将项目的公共目录重命名为 public_html。

上述对 index.php 文件的编辑将指导 Laravel 项目使用该文件的直接父目录作为项目的公共目录。

您可以在此出版物中找到更多信息: https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5

【讨论】:

    猜你喜欢
    • 2014-03-18
    • 2019-06-27
    • 2018-05-03
    • 1970-01-01
    • 2018-09-20
    • 2017-08-22
    • 2013-06-05
    • 2018-08-14
    • 2017-04-12
    相关资源
    最近更新 更多