我正在Laravel 5.8上建立我的第一个站点,并且在大多数情况下,它们可以正常工作。我可以创建一个新的帖子,上传照片,点击保存,并且记录将保存到数据库中,并且可以在视图,图像和所有视图中看到帖子数据。我将所做的更改推送到生产服务器,创建了新的Post,图像等...,看起来一切正常。直到我在本地环境中进行另一次更改,然后我才意识到我的public / uploads目录已被清除,并且上传到Post的所有图像都有损坏的图像链接。
我的设置
Laravel 5.8(本地和生产),MySql 5.7(本地和生产),代客(本地),推送到Bitbucket,Envoyer拉动更改和自动部署,通过Forge设置的Digital Ocean Droplet / p>
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'));
}
答案 0 :(得分:2)
此文件夹可能未版本。
在文件夹(公用/上传)内添加一个空的.gitkeep
文件,提交,推送和重做部署。
Git不会保留空文件夹。