简体   繁体   中英

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. 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. 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.

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

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

This folder is probably NOT VERSIONED .

Add an empty .gitkeep file inside the folder (public/uploads), commit, push and redo the deploy.

Git doesn't keep empty folders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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