0
votes

i'm having troble deleting a file from a Laravel Project. The file is in the /storage/exports directory, it's an excel stored on the disk usingthe Laravel Excel library. This is my code:

$path = $excel->store('xls', false, true)['full'];
...send the xls via mail....
Storage::delete($path);

When i check the existence of the file with file_exist i get true, so Laravel is able to read the file. I've also checked the permission for the folder and i give all permissions on this folder using chmod 777 Any ideas?

Thanks!

3
After using Laravel I've learned one thing... EASY became DIFFICULT - Martin Zvarík
try using File - Chamara Abeysekara
use "File" facade to manipulate files stored outside storage/app folder - Ravisha Hesh

3 Answers

1
votes

The storage driver already has an awareness of a root directory, so the $path must be relative, not full. So if your file is in: /this/is/the/full/path.xls, and the config filesystems.disks.local.root is set to /this/is/the/full, you're essentially having it look for a file in /this/is/the/full/this/is/the/full/path.xls.

You have two options.

1) Add a new driver to that config, and reference it directly:

'custom_location' => [
    'driver' => 'local',
    'root' => '/some/other/root/path',
]

Storage::driver('custom_location')->delete($relativePathFromRoot)

2) Create a one-off:

$rootPath = '/some/other/root/path';
$client = Storage::createLocalDriver(['root' => $rootPath]);
$client->delete($relativePathFromRoot);
0
votes

Try storing the file within storage/app/exports. The Storage class's default location to store files is storage/app.

It may be a good idea to instead of using the Excel class to store the file, to instead save the file using the Storage class: Storage::put('exports/excelfile.xls', $fileContents);

0
votes

Even though you are using Laravel, you can use regular PHP functions.

unlink($path);