Laravel Storage :: download在路径中找不到文件

时间:2019-07-15 12:57:04

标签: laravel file download storage

我正在尝试下载我上传的文件(rapports),但是尽管定义了路径并且看起来正确,但是却出现File not found at path错误。我已经使用php artisan storage:link命令创建了符号链接。

这是我存储rapports

的方式
$rapport = new Rapport;
        $rapport->themeRapport = $request->input('themeRapport');
        $rapport->resume = $request->input('resume');
        $rapport->auteur = $request->input('auteur');
        $rapport->encadrant = $request->input('encadrant');
        $rapport->cycle = $request->input('cycle');
        $rapport->filiere = $request->input('filiere');

        $fichierRapport = $request->file('fichierRapport');
        $fichierRapportName = $request->input('themeRapport') .'.'. $fichierRapport->getClientOriginalExtension();
        $path = $fichierRapport->storeAs('rapports', $fichierRapportName);

     $rapport->fichierRapport = $fichierRapportName;
     $rapport->save();

这是rapports表结构

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRapportsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('rapports', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('themeRapport');
            $table->string('resume');
            $table->string('auteur');
            $table->string('encadrant');
            $table->string('cycle');
            $table->string('filiere');
            $table->string('fichierRapport'); // File name of the uploaded file
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('rapports');
    }
}

然后,当我将storage/app/public/rapports磁盘设置为默认磁盘时,上传的文件将转到新创建的public目录。

config/filesystems.php文件

<?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', 'public'),

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

    ],

];

为了下载,我已经尝试过:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Rapport;
use Illuminate\Support\Facades\Storage;


class downloadRapport extends Controller
{

    public function download($id){
        $rapport = Rapport::findOrFail($id);
        $url = Storage::url($rapport->fichierRapport);
        return Storage::download($url, $rapport->fichierRapport);
    }
}

这使我遇到此错误:File not found at path: http:/localhost/storage/filename

我也尝试过

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Rapport;
use Illuminate\Support\Facades\Storage;


class downloadRapport extends Controller
{

    public function download($id){
        $rapport = Rapport::findOrFail($id);
        $url = Storage::disk('public')->url($rapport->fichierRapport);
        return Storage::download($url, $rapport->fichierRapport);
    }
}

我遇到了相同的错误:File not found at path: http:/localhost/storage/filename

然后我尝试了以下操作,因为文件被上传到新创建的目录rapports

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Rapport;
use Illuminate\Support\Facades\Storage;


class downloadRapport extends Controller
{

    public function download($id){
        $rapport = Rapport::findOrFail($id);
        $url = Storage::disk('public')->url('rapports/'.$rapport->fichierRapport);
        return Storage::download($url, $rapport->fichierRapport);
    }
}

错误:File not found at path: http:/localhost/storage/rapports/filename

要获得完整的路径,我发现here,我尝试过

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Rapport;
use Illuminate\Support\Facades\Storage;


class downloadRapport extends Controller
{

    public function download($id){
        $rapport = Rapport::findOrFail($id);
        $url = Storage::disk('public')->path('rapports/'.$rapport->fichierRapport);

        return Storage::download($url, $rapport->fichierRapport);
    }
}

错误:File not found at path: C:/xampp/htdocs/MonProjet/storage/app/public/rapports/filename 此完整路径是文件实际所在的位置

代码有什么问题..?我在那里缺少什么吗?这是我第一次在Laravel中操作文件,非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以简单地运行以下命令:

php artisan storage:link

您的文件将显示在root/public/storage/your_folder中 您只需将href写入该文件即可。

<a href="{{asset('/storage/folder/'.$file)}}">File Name</a>