How to delete a file using delete_files function of File Helper - codeigniter

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

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'

Issue getting a file with Laravel

Hi I do not know what is happening but I want to get a file from the storage folder and it says that it does not existe but it exists I mean I go to the folder and it is there..
I retrieve the file like this:
$path = storage_path('app/public/' . $settlement->support);
$settlement_file = Storage::disk('local')->get($path);
And I get this error:
message: "File not found at path:
home/jysparki/newjis.jisparking.com/storage/app/public/06152617_8-2025.pdf"
and If I go to the folder it exists so I wonder what am I doing wrong? Thanks.
If you look at config/filesystems.php file and find disks array, you may notice that root under the local disk key is set to storage_path('app'). Well it's up to what the value may be in your case you're free to change it, but what does this mean is whenever you call Storage::disk('local') method it will try to find files relative to storage_path('app'), so there is no need to specify full path. Try this instead
$settlement_file = Storage::disk('local')->get('/public/' . $settlement->support);
or
$settlement_file = Storage::disk('public')->get($settlement->support);
as Laravel by default has public disk which pointed into /app/public storage directory

Delete a file from specific directory 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()

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 do I get the full local path of a file uploaded with $_FILES

I am uploaded a file through the file input. If I print_r($_FILES), I can get several pieces of info, but it doesn't give me the full LOCAL path of the file (the path on my computer, not the server). How do I get this?
I need this to use the FTP library in CodeIgniter. Documentation is here on how to use it to upload a file.
As you can see, it requires the full local path, although I'm not sure why.
As file upload path is a variable which changes from server to server, I would suggest you to use move_uploaded_file() function:
$target = "uploads/myfile.something";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) {
// do something
}
that way it will always work no matter what the path is even if it changes. The target can be anything you wish, it's relative to the current folder where script is being executed.
I solved this.
public function upload($file, $folder) {
$this->CI->ftp->upload($_FILES['file']['tmp_name'], '/public_html/rest/of/path/' . $_FILES['file']['name'], 'ascii', 0775);
}
The file is being uploaded, so you can access it with $FILES['file']['tmp_name']. This is from within a class, so that's why I'm using $this->CI. Otherwise, I would just use $this.

Resources