![](/img/trans.png)
[英]Laravel 5.8 - How to move file from storage directory to public directory?
[英]Deploying to production with Laravel 5.8 deletes my public uploads storage directory
我正在Laravel 5.8上構建我的第一個站點,並且大多數情況下它們都可以正常工作。 我可以創建一個新的帖子,上傳照片,點擊保存,並且記錄將保存到數據庫中,並且可以在視圖,圖像和所有視圖中看到帖子數據。 我將所做的更改推送到生產服務器,創建了新的Post,圖像等...,看起來一切正常。 直到我從本地環境中進行另一次更改,然后我才意識到我的public / uploads目錄已被清除,並且上傳到Post的所有圖像都有損壞的圖像鏈接。
我的設定
Laravel 5.8(本地和正式版),MySql 5.7(本地和正式版),Valet(本地),推送至Bitbucket, Envoyer進行更改和自動部署,通過Forge設置的Digital Ocean Droplet
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'));
}
此文件夾可能未版本化 。
在文件夾(公用/上載)內添加一個空的.gitkeep
文件,提交,推送和重做部署。
Git不會保留空文件夾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.