英文:
Deleting files with Laravel Facade
问题
我正在尝试使用Storage Facade删除文件,但无法使其工作:
$filePath = 'app/data-export/';
$excelFile = storage_path($filePath . 'ExcelData.xlsx');
$deleted = Storage::disk('local')->delete($excelFile);
文件存在于磁盘上:
当我运行代码时,没有抛出错误,$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:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论