使用Laravel Facade删除文件

huangapple go评论60阅读模式
英文:

Deleting files with Laravel Facade

问题

我正在尝试使用Storage Facade删除文件,但无法使其工作:

$filePath = 'app/data-export/';
$excelFile = storage_path($filePath . 'ExcelData.xlsx');
$deleted = Storage::disk('local')->delete($excelFile);

文件存在于磁盘上:

使用Laravel Facade删除文件

当我运行代码时,没有抛出错误,$deleted 返回true,但文件未被删除。

我也尝试过:

$deleted = Storage::delete($excelFile);

但这也不起作用。

我的filesystems.php配置如下:

'disks' => [

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

    'public' => [
        'driver'     => 'local',
        'root'       => storage_path('app/public'),
        'url'        => env('APP_URL').'/storage',
        'visibility' => 'public',
        'throw'      => false,
    ],

    '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'),
        'endpoint'                => env('AWS_ENDPOINT'),
        'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
        'throw'                   => false,
    ],

],

PHP 8.2.0,Laravel 9.52.10,Windows 10 Home。

英文:

I'm trying to delete a file using the Storage Facade but can't get it to work:

$filePath = 'app/data-export/';
$excelFile = storage_path($filePath . 'ExcelData.xlsx');
$deleted = Storage::disk('local')->delete($excelFile);

The file is there on the disk:

使用Laravel Facade删除文件

When I run the code, no errors are thrown and $deleted comes back true, but the file is not removed.

I've also tried

$deleted = Storage::delete($excelFile);

but that doesn't work either.

My filesystems.php is

'disks' => [

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

    'public' => [
        'driver'     => 'local',
        'root'       => storage_path('app/public'),
        'url'        => env('APP_URL').'/storage',
        'visibility' => 'public',
        'throw'      => false,
    ],

    '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'),
        'endpoint'                => env('AWS_ENDPOINT'),
        'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
        'throw'                   => false,
    ],

],

PHP 8.2.0, Laravel 9.52.10, Windows 10 Home.

答案1

得分: 0

"The point of the Storage facade is that it knows its own path. You do not have to provide the full path; instead, this solution should suffice.

$excelFile = 'data-export/' . 'ExcelData.xlsx';
$deleted = Storage::disk('local')->delete($excelFile);"
英文:

The point of the Storage facade, is it knows it own path. You do not have to provide the full path, instead this solution should suffice.

$excelFile = 'data-export/' . 'ExcelData.xlsx';
$deleted = Storage::disk('local')->delete($excelFile);

huangapple
  • 本文由 发表于 2023年7月17日 16:08:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定