0

我正在 Laravel 5.8 上构建我的第一个站点,并且大多数情况下都可以正常工作。我可以创建一个新帖子,上传一张照片,点击保存,记录会保存到数据库中,我可以在我的视图、图像和所有内容中看到帖子数据。我将我的更改推送到我的生产服务器,创建一个新的帖子、图像等……一切似乎都很好。直到,我从本地环境推送另一个更改,然后我意识到我的 public/uploads 目录已被清除,并且任何上传到 Post 的图像都有一个损坏的图像链接。

我的设置

Laravel 5.8(本地和生产)、MySql 5.7(本地和生产)、Valet(本地)、Push to Bitbucket、Envoyer拉取更改和自动部署、通过Forge设置的 Digital Ocean Droplet

文件系统.php

...
'public' => [
    'driver' => 'local',
    'root' => public_path(),
    'url' => env('APP_URL').'/public',
    'visibility' => 'public',
],
...

PostController.php

public function store(Request $request)
{

    $rules = [
        'title' => ['required', 'min:3'],
        'body' => ['required', 'min:5'],
        'photo' => 'image|mimes:jpeg,png,jpg,gif|max:4096M'
    ];
    $request->validate($rules);
    $user_id = Auth::id();
    $post = new Post();
    $post->user_id = $user_id;
    $post->is_featured = $request->input('is_featured', false);
    $post->is_place = $request->input('is_place', false);
    $post->title = request('title');
    $post->body = request('body');
    $post->place_name = request('place_name');
    $post->place_address = request('place_address');
    $post->place_city = request('place_city');
    $post->place_state = request('place_state');
    $post->place_zip = request('place_zip');
    $post->place_phone = request('place_phone');
    $post->place_email = request('place_email');
    $post->place_website = request('place_website');

    if ($request->has('photo')) {
        // Get image file
        $image = $request->file('photo');
        // Make a image name based on user name and current timestamp
        $name = Str::slug($request->input('name')).time();
        // Define folder path
        $folder = '/uploads/posts/' . $user_id . '/';
        // Make a file path where image will be stored [ folder path + file name + file extension]
        $filePath = $folder . $name. '.' . $image->getClientOriginalExtension();
        // Upload image
        $this->uploadOne($image, $folder, 'public', $name);
        // Set user profile image path in database to filePath
        $post->photo = $filePath;
    }

    $post->save();

    $posts = Post::all();
    return view('backend.auth.post.index', compact('posts'));
}
4

1 回答 1

2

此文件夹可能未版本化

在文件夹 (public/uploads) 中添加一个空.gitkeep文件,提交、推送并重做部署。

Git 不会保留空文件夹。

于 2019-05-07T16:09:22.640 回答