【问题标题】:Laravel 5 - I can't retrieve file uploaded even if they are actually uploadedLaravel 5 - 即使它们实际上已上传,我也无法检索上传的文件
【发布时间】:2018-06-15 06:02:03
【问题描述】:

我正在尝试创建一个简单的文件上传器,实际上文件已上传,我也可以删除它们,但实际上我无法访问它们。 我给你举个例子:如果我想上传一张图片,一切似乎都很好(I can also return the Url after upload happened),正如你在screenshot 中看到的那样。
但是,当我尝试访问该网址时,我会得到一个 error,或者如果我尝试将网址插入为<img> 的 src,图片不会加载。

请注意,我已经运行了php artisan storage:link,正如您在第二张图片中看到的那样。我使用 Homestead 和 Virtual box 作为开发环境。

这是一些代码:
来自我的上传控制器

public function uploadFile(Request $request){
    $file = $request->file('file_field');
    $ext = $file->guessClientExtension();

    $validator = Validator::make(Input::all(), [
        'file_field' => 'required',
        'comment' => 'required',
        'confidentiality' => 'required',
    ]);

    if ($validator->fails()) {
        return Redirect::back()
                    ->withErrors($validator)
                    ->withInput();
    }

    $file_model = File::create([
        'id_paziente' => Input::get('idPatient'),
        'id_audit_log' => Input::get('idLog'),
        'id_file_confidenzialita' => Input::get('confidentiality'),
        'file_nome' => $file->getClientOriginalName(),
        'file_commento' => Input::get('comment'),
    ]);

    $file_model->save();
    return Storage::disk('public')->putFileAs("/patient/".Input::get('idPatient'), $file, $file->getClientOriginalName(), 'public');
}


我的配置/文件系统.php

<?php

return [

'default' => 'public',

'cloud' => env('FILESYSTEM_CLOUD', 's3'),

'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'),
    ],

],

];

【问题讨论】:

    标签: php laravel file file-upload storage


    【解决方案1】:

    Documentation for File Storage

    我有同样的问题,我用这个方法来存储我的上传图片 url

    Storage::url ($file);
    

    并将其显示到 src for img 上的刀片中

    【讨论】:

    • 嗯,对我来说这是工作:/祝你好运,很抱歉没有帮助你更多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 2016-07-21
    • 1970-01-01
    • 2018-06-11
    • 2016-08-11
    • 2016-04-18
    相关资源
    最近更新 更多