簡體   English   中英

Laravel:存儲文件時“無法創建根目錄”

[英]Laravel: "Impossible to create the root directory" when storing file

我正在嘗試通過 AJAX 將文件存儲到 laravel 的公用文件夾中,但是當我提交表單時,我收到以下消息:

" 消息:"無法創建根目錄 \"C:\xampp\htdocs\Restaurante1\storage\app\C:/xampp/htdocs/Restaurante1/public/img\"。",異常:"League\Flysystem\Exception ", 文件: "C:\xampp\htdocs\Restaurant1\vendor\league\flysystem\src\Adapter\Local.php"

我正在嘗試將文件存儲到公共目錄內的以下文件夾中:public > img >uploads

這是我的 JQuery 代碼:

 console.log('new slider button clicked'); var formData = new FormData(); formData.append('title', $('#sliders_title').val()); formData.append('body', $('#sliders_body').val()); formData.append('isVisible', $('#sliders_isVisible').is(':checked')? 1: 0); formData.append('image', $('#sliders_image').prop('files')[0]); $.ajax({ async: true, url: '/sliders', type: 'POST', data: formData, dataType: 'JSON', processData: false, contentType: false, success: function (data) { $('.form_valid_container').html('<span class="form_valid_text">✓ '+ data.success +'</span>'); form.trigger("reset"); console.log(data.success, data.errors); }, error: function (data){ var errors = data.responseJSON; console.log(errors); $.each(errors, function(){ $('.form_error_container').html('<span class="form_error_text">✘ '+ errors.message +'</span>') }); } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

我的 controller 方法:

 public function store(StoreSlider $request) { $uploadFile = $request->file('image'); //generate random filename and append original extension (eg: asddasada.jpg, asddasada.png) $filename = str_random(6).'.'.$uploadFile->extension(); // storing path (Change it to your desired path in public folder) $path = 'img/uploads/'; // Move file to public filder $uploadFile->storeAs(public_path($path), $filename); $slider = new Slider(); $slider->title = $request->title; $slider->body = $request->body; $slider->image = $path.'/'.$filename; // So that you can access image by url($slider->image); $slider->isVisible = $request->isVisible; $slider->save(); return response()->json([ 'success' => 'Diapositiva guardada correctamente', 'slider' => $slider, ]); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

編輯:這是我的配置/文件系統

 <?php return [ /* |-------------------------------------------------------------------------- | Default Filesystem Disk |-------------------------------------------------------------------------- | | Here you may specify the default filesystem disk that should be used | by the framework. The "local" disk, as well as a variety of cloud | based disks are available to your application. Just store away, | */ 'default' => env('FILESYSTEM_DRIVER', 'local'). /* |-------------------------------------------------------------------------- | Default Cloud Filesystem Disk |-------------------------------------------------------------------------- | | Many applications store files both locally and in the cloud, For this | reason. you may specify a default "cloud" driver here. This driver | will be bound as the Cloud disk implementation in the container, | */ 'cloud' => env('FILESYSTEM_CLOUD', 's3'), /* |-------------------------------------------------------------------------- | Filesystem Disks |-------------------------------------------------------------------------- | | Here you may configure as many filesystem "disks" as you wish. and you | may even configure multiple disks of the same driver. Defaults have | been setup for each driver as an example of the required options: | | Supported Drivers, "local", "ftp", "sftp", "s3", "rackspace" | */ 'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'). 'url' => env('APP_URL'),'/storage', 'visibility' => 'public', ], 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), ], ]; ]
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

我的回答可能與這個問題無關,但如果有人遇到我認為可能有幫助的相同問題,我的回答應該是相關的。 我遇到了同樣的問題,我“ putFileAs ”的工作方式與“ storeAs ”相同,就像這樣$path = Storage::putFileAs('public/users_upload/'.$user->username, $File_to_be_moved, $file_name_to_store_as); 第一個參數是您目錄的路徑...您可以在此處查看laravel文檔以獲取更多信息

在您的文件系統中,您需要創建驅動程序磁盤。

'uploads' => [
        'driver' => 'local',
        'root' => public_path('img/uploads'),
        'url' => '/img/uploads',
        'visibility' => 'public',
    ],

你可以調用你的控制器存儲

$uploadFile->storeAs('/', $filename, 'uploads');

首先,你應該知道你在做什么是不好的做法。 但是你也應該知道,這個錯誤並不是因為 Laravel 做不到。 這是因為 Laravel 找不到路徑“img/uploads”。

解決方案 1:手動創建目錄,如果您使用存儲庫管理器,請不要忘記在其中添加 .gitignore 文件(如果您使用的是 Git)。

解決方案2:

后:

// storing path (Change it to your desired path in public folder)
$path = 'img/uploads/';

添加:

if(!file_exists(public_path($path)) || !is_dir(public_path($path)){
    @mkdir(public_path($path));
}

解決方案 3(Laravel 建議我們如何做。):

由於您使用的是 windows,您可以創建一個符號鏈接(實際上是一個硬鏈接,但仍然是相同的。)

以管理員身份運行命令提示符 (cmd) 並輸入:

mklink /J "laravel/public/file/directory/uploadsdirectory" "/path/to/laravel/roor/storage/app"

之后,您可以更改此設置:

$uploadFile->storeAs(public_path($path), $filename);

到:

$uploadFile->storeAs($path, $filename);

注意:我沒有嘗試過這些代碼/解決方案,但它們應該可以完美運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM