如何将其原始名称的文件保存在公用文件夹中

时间:2019-06-24 11:37:55

标签: laravel eloquent laravel-5.8

我试图将文件从默认位置保存到新位置(在公共/文件中),并想用其原始名称保存它们,我更新了代码,但该代码不起作用:

我从上一个更新的代码:

    public function store(Request $request)
    {
    request()->validate([
        'filename' => 'required',
    ]);
    $files = $request->hasfile('filename');
    $files->move(public_path('files'), $new_name);
    foreach($request->file('filename') as $file) {
            File::create([
                'filename' => $file->getClientOriginalExtension(),
        ]);
    }

    return redirect('/file')->with('success', 'File Uploaded Successfully');
 }

我的原始/当前工作代码:

  public function store(Request $request)
  {
    request()->validate([
        'filename' => 'required',
    ]);
    $files = $request->file('filename');
    foreach ($files as $file) {
            File::create([
                'filename' => $file->getClientOriginalExtension(),
        ]);
    }

    return redirect('/file')->with('success', 'File Uploaded Successfully');
}

我该怎么做?

2 个答案:

答案 0 :(得分:1)

基本上,您需要做的是:

  1. 配置Laravel使用“ 公共”文件夹存储文件。您可以通过编辑 config / filesystems.php 并将以下代码添加到 disks 数组中来实现:
        'disks' => [
            // This is the block to add
            'my-disk' => [
                'driver' => 'local',
                'root' => public_path(), // Here we tell, that we want to save to the **public** folder
                'url' => env('APP_URL'),
                'visibility' => 'public',
            ],
            // Block end

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

        ],
  1. 将文件保存到此新磁盘:
if ($request->hasFile('filename')) {
    $file = $request->file('filename');
    $path = Storage::disk('my-disk')->putFileAs('files', $file, $file->getClientOriginalName()); // Here the first argument for putFileAs is the subfolder to save to
}

答案 1 :(得分:0)

已更新

问题1 :使用laravel已采用的关键字,例如文件

解决方案:重命名模型文件以及迁移示例:-

模型文件: UploadedFile.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UploadedFile extends Model
{
    protected $fillable = [
        'filename',
        'path'
    ];
    protected $table = 'uploadedFiles';
}

迁移文件: 2019_06_24_072153_create_uploaded_files_table.php

<?php

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

class CreateUploadedFilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('uploadedFiles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('filename');
            $table->string('path');
            $table->timestamps();
        });
    }

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

问题2 :在公用文件夹中未创建符号链接(强烈建议将文件存储在存储文件夹中,而不仅仅是公用)

解决方案:删除已创建为存储的文件夹并运行此命令

php artisan storage:link

现在在 FileController 中,这是将多个图像存储到存储文件夹和数据库中的方式:-

public function store(Request $request) 
{ 
    request()->validate([
    'filename' => 'required',
    ]);
    if($request->hasFile('filename')){
        $files = $request->file('filename');
        foreach($files as $file){
            $filename = $file->getClientOriginalName();
            Storage::disk('public')->put('files/'.$filename, File::get($file));
            UploadedFile::create([
                'filename' => $filename,
                'path'     => url('storage/files/'.$filename)
            ]);
        }
    }
    return redirect('/file')->with('success', 'File Uploaded Successfully');
} 

在刀片文件的简单调用路径中,因为我们已经将路径保存到数据库中

<img class="card-img-top" src="{{ $file->path }}">

进行所有更改后,请运行以下命令

composer update
php artisan config:cache
composer dumpa
php artisan migrate:refresh --seed

如果您使用的是Ubuntu,则还需要授予权限

sudo chmod -R a+rwx  /opt/lampp/htdocs/multifiles2/
chmod 777 /opt/lampp/htdocs/multifiles2/storage/logs/laravel-2019-06-24.log //In case if log file is not accessible