【问题标题】:Save files using Storage::put and XMLHttpRequest in Laravel 5.3在 Laravel 5.3 中使用 Storage::put 和 XMLHttpRequest 保存文件
【发布时间】:2017-05-30 08:52:40
【问题描述】:

我正在尝试使用 DRAG & DROP 和 XMLHttpRequest 发送文件。

$images = $_FILES['images'];

当我使用 foreach 时:

foreach($images["name"] as $file => $name)

move_uploaded_file($images["tmp_name"][$file], $images_dir . $name

它工作正常,但我想使用

Storage::put

但它不起作用

我有:

use Illuminate\Support\Facades\Storage;

filesystems.php

'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'visibility' => 'public',
        ],

当我将鼠标光标指向我的 PHPStorm IDE 中的“放置”时 "Method 'put' not found in Illuminate\Support\Facades\Storage"

class Storage 扩展了 Facade,但在 Facade 中也没有 'put' 方法。 Storage 有什么问题?

【问题讨论】:

    标签: javascript php laravel xmlhttprequest laravel-5.3


    【解决方案1】:

    不要说使用Illuminate\Support\Facades\Storage,而是说use Storage

    然后忽略任何 IDE 错误并继续查看其是否正常工作。 Laravel 使用魔术方法和许多其他架构模式。并非所有 IDE 都能正确评估其代码,因此请忽略 IDE 错误

    也避免使用 $_FILES。使用以下内容:

    $files = $request->input('images');
    foreach($files as $key => $value) {
        if ($request->hasFile('images.' . $key)) {
            $file = $request->file('images.' . $key);
            $filename = $file->getClientOriginalName();
            Storage::put($filename, file_get_contents($file));
        }
    }
    

    【讨论】:

    • 感谢您的建议,但我仍然无法解决问题 Storage::put。我认为问题可能出在 $_FILE 中用作 XMLHttpRequest 中的文件名,因为当我使用从表单接收的 Reguest 数据时 -> 输入类型“文件”工作正常。
    猜你喜欢
    • 2017-02-21
    • 1970-01-01
    • 2022-07-29
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2017-02-12
    • 1970-01-01
    相关资源
    最近更新 更多