简体   繁体   English

使用Laravel 5.8部署到生产中会删除我的公共上传存储目录

[英]Deploying to production with Laravel 5.8 deletes my public uploads storage directory

I am building my first site on Laravel 5.8 and for the most part things work as they should. 我正在Laravel 5.8上构建我的第一个站点,并且大多数情况下它们都可以正常工作。 I can create a new Post, upload a photo, hit save and the record persists to the database and I can see the Post data in my view, image and all. 我可以创建一个新的帖子,上传照片,点击保存,并且记录将保存到数据库中,并且可以在视图,图像和所有视图中看到帖子数据。 I push my changes to my production server, create a new Post, image, etc... and things seem to work great. 我将所做的更改推送到生产服务器,创建了新的Post,图像等...,看起来一切正常。 Until, I push another change from my local environment, then I realized my public/uploads directory is wiped out and any images uploaded to a Post has a broken image link. 直到我从本地环境中进行另一次更改,然后我才意识到我的public / uploads目录已被清除,并且上传到Post的所有图像都有损坏的图像链接。

My Setup 我的设定

Laravel 5.8 (local and production), MySql 5.7 (local and production), Valet (local), Push to Bitbucket, Envoyer Pulls Changes and Auto deploys, Digital Ocean Droplet set via Forge Laravel 5.8(本地和正式版),MySql 5.7(本地和正式版),Valet(本地),推送至Bitbucket, Envoyer进行更改和自动部署,通过Forge设置的Digital Ocean Droplet

filesystems.php filesystems.php

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

PostController.php 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'));
}

This folder is probably NOT VERSIONED . 此文件夹可能未版本化

Add an empty .gitkeep file inside the folder (public/uploads), commit, push and redo the deploy. 在文件夹(公用/上载)内添加一个空的.gitkeep文件,提交,推送和重做部署。

Git doesn't keep empty folders. Git不会保留空文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM