Delete a file from specific directory Laravel - laravel

I have some files in "public/uploads/somephoto.jpg". How to delete this file? I tried
unlink(url('/uploads/somefile.jpg'));
But it doesn't work.

if file in your public folder then
unlink(public_path('uploads/somefile.jpg'));
or if it is stored in storage folder then
unlink(storage_path('uploads/somefile.jpg'));
here url() retrun http url but for delete file edit file you need system path which will be get by storage_path() or public_path()

Related

Laravel storage exists() returns false for file names with spaces

I'm trying to check if file exists using storage exists() function.
This function returns true if file name doesn't have spaces but false otherwise.
I'm trying to use Storage facades code as follows
if (Storage::disk("local")->exists($model->path)) { // path of the file
Storage::move(); // logic to move files
}
Storage folders has all permissions
I have lot of files with spaces in my storage subfolder F1. I want to move those file to another subfolder F2 inside storage itself. It would be tedious to rename each and every file for exists() to work.
Update
Php inbuilt file_exists() works for same path i.e it returns true if file is found and even if it has spaces in it's name.
Code for file_exists()
file_exists(storage_path() . '/app/F1/Demo One.pdf');
I have used tinker for debugging.
Why storage facades exists() returns false?
storage_path() and the local Storage disk have different base directories
storage_path() base directory is 'storage'
Storage::disk('local') base directory is 'storage/app'

Laravel 5.5 Storage::url() doesn't return the desired url with S3

I'm using S3 for storage in my laravel 5.5 project
Everything is good and the images is stored in my bucket under my desired folder
using :
use Illuminate\Support\Facades\Storage;
$path = Storage::put($directory, $file, $permission);
$fullPath = config('filesystems.disks.s3.driver').
'.'.
config('filesystems.disks.s3.region').
'.amazonaws.com/'.
config('filesystems.disks.s3.bucket').
'/'.
$path;
the full path then will be like :
https://s3.us-east-2.amazonaws.com/iva-files/brands/logos/qweisiEC7H2SOV9ozjuOOBRxw399cTm2imnbJEXj.jpeg
I searched for better way to get my full url so I found:
Storage::url($path);
But it's returning path like :
https://ivasystem.signin.aws.amazon.com/console/brands/logos
What's wrong with my sinario ?
Try:
Storage::disk('s3')->url($filename)
As mentioned in the Official Laravel Documents
The path to the file will be returned by the putFile method so you can store the path, including the generated file name, in your database.
So you should always store file path & file name in database columns so that later on you can concatenate path & name to get fill url to the file.
In my project I am already doing this.

How to set path to file in Laravel?

I have located file in root directory of Laravel.
From controller I try to get this file like as:
$shipments = json_decode(file_get_contents("Statistics.json"), true);
But I get error.
What correct path to specify for getting file?
https://laravel.com/docs/5.0/helpers#paths
I guess you need base_path
file_get_contents(base_path().'/Statistics.json');

How to delete a file using delete_files function of File Helper

I would like to delete a file in codeigniter.
I want to use delete_files function, but it returns false !
$this->load->helper("file");
return delete_files(base_url().'application/cache/'.'blade-'.$cache_path);
Why it is returning false instead of deleting files.
Just use :
return unlink(APPPATH.'cache/'.'blade-'.$cache_path);
use
$this->load->helper('file');
return delete_files(FCPATH.'application/cache/'.'blade-'.$cache_path);
Codeigniter delete_files function will delete files inside a directory and it returns true or false.
Returns:TRUE on success, FALSE in case of an error
You getting False means you have error.
Possible reasons for errors
Folder does not exists.
Folder path is not valid
Does not have proper permission
The files must be writable or owned by the system in order to be
deleted.
For your case base_url().'application/cache/'.'blade-'.$cache_path is not a valid folder path.you added base_url before your folder name which will make the folder name like an URL. But It should be folder path.
Use FCPATH instead of base_url
you need to change the path to the folder delete like
$this->load->helper('file');
return delete_files(APPPATH.'cache/'.'blade-'.$cache_path);
you donĀ“t need to writte the url, coz the APPPATH places the pointer on the folder application, then you only specify in what subfolder you want to delete the file

how to delete file in joomla2.5

I have dynamic listing page. From that I want to delete file from server using joomla2.5. I try with the following code:
jimport('joomla.filesystem.file');
$path = JPath::clean($path);
It is not working.
To delete the folder:
JFolder::delete(JPATH_ROOT.DS.'modules'.DS.'mod_footer');
To delete the File:
JFile::delete(JPATH_ROOT.DS.'modules'.DS.'mod_footer'.DS.'mod_footer.php');

Resources