how to check if folder exists cakephp - cakephp

Is there any instance to return bool? e.g. $folder->exists() => bool
$path = WEBROOT_DIR .'/files/'. $folder_name;
$folder = new Folder('../'.$path);
//DOESN'T WORK
debug($folder->exists());
//I can do it in this way
//returns bool
debug(file_exists($folder->path));
I want to check if directory exists delete it. but I want to use specifically cake method to check if folder exists

Try this:
$path = 'files' . DS . $folder_name;
$folder = new Folder($path);
if (!is_null($folder->path)) {
$folder->delete();
}

Cakephp Folder class does not have method exists like File class has.
You could check if the folder exists with standard php and then delete it.
if (file_exists('path/to/directory')) {
rmdir('path/to/directory', 777, true);
}
Basically it checks if the folder exists with the file_exists function and if the file does not exist then it will use rmdir function to create the folder.
Though I think you could use cakephp like this. This will delete the folder even if the folder does not exist because the Folder constructor will create the folder if the folder does not exist.
$folder = new Folder('path/to/directory', true, 777);
$folder->delete();

Related

How to create folder base on users input using cakephp3

$user= $this->Users->newEntity();
I want to create a folder for every registered user
example if new user input michael
how to achieve this and will create a directory /img/users/michael/
$dir = new Folder(WWW_ROOT.'img'.DS.'users'.$user->username);
$path_data = $dir->create($dir);
Error: Cannot use object of type Cake\Filesystem\Folder as array
Create directory PATH will in in create() method
$dir = new Folder(WWW_ROOT.'img'.DS.'users'.$user->username);
$path_data = $dir->create($dir);
should be
$dir = new Folder();
$path_data = $dir->create(WWW_ROOT.'img'.DS.'users'.$user->username);
Details Here
Update
There is a directory separator missing. it should like
$dir = new Folder();
$path_data = $dir->create(WWW_ROOT.'img'.DS.'users'.DS.$user->username);
Thanks #ndm

CakePHP 3: How to check if file or image exists

I am just trying to check if an image exists or not, I can do it by using PHP. For example:-
$file = WWW_ROOT .'uploads' . DS . 'employee' . DS .'_'.check.jpg;
$file_exists = file_exists($file);
It's working fine for me. But I have tried also tried using elementExists like this:-
if($this->elementExists("../".$employees->front_image))
{
echo $this->Html->image("../".$employees->front_image); // image output fine without condition.
}
// Here $employees->front_image = uploads/employee/employeename.jpg
This check is not working. How can I do this in CakePHP?
CakePHP is written in PHP, so if you already have a simple solution like file_exists() use that. So you can write something like this:-
if (file_exists(WWW_ROOT . $employees->front_image)):
echo $this->Html->image('../' . $employees->front_image);
endif;
elementExists() is intended for checking that a View element exists, not if files exist in the webroot, so should not be used like you are trying. It does do a file_exists() check, but this scans all available View element paths only.
I think this works in Cake 3 (you should do this in afterFind method IMO):
// Create a new file with 0644 permissions
$file = new File('/path/to/file.php', true, 0644);
if ($file->exists()) {
//do something
}
$file->close();
Your way is checking whether view element exists or not.
This worked for me:
Q: How to check if image exists on remote url or not?
Explanation:
$rfile will take url
$check will open the file with read rights
If file exists then it will print "File exists" else "Doesn't exists".
code:
<?php
// Remote file url
$rFile = 'https://www.example.com/files/test.pdf';
// Open the file
$check = #fopen($rFile, 'r');
// Check if the file exists
if(!$check){
echo 'File does not exist';
}else{
echo 'File exists';
}
?>

Create folder in explicit directory AIR

Like the title says I want to create a folder in a specific directory with Adobe Air.
If I use static methods of File like File.userDirectory works fine but I need to give the choice to select the directory.
I am trying this:
file.addEventListener(Event.SELECT, dirSelected);
file.browseForDirectory("Select a directory");
function dirSelected(e:Event):void {
trace(file.nativePath);
file.resolvePath("new_folder");
file.createDirectory();
}
Nothing happens
"resolvePath: Creates a new File object with a path relative to this File object's path, based on the path parameter (a string)."
So:
var newDir:File = file.resolvePath("new_folder");
newDir.createDirectory();

Deleting images from a folder programmatically

i want to delete images from public:// sub folder pro grammatically. i have paths like this below.
$path = 'http://localhost/Drupal/dbquery/sites/default/files/take_snap/20140713110549.jpg';
i want to delete like this,
$path = 'http://localhost/Drupal/dbquery/sites/default/files/take_snap/20140713110549.jpg';
$del = unlink($path);
but it doesn't delete image. i try to do this from menu call back function. how do i do that?.
In Drupal 7 you can use the file_delete to delete the file and its DB records as well.
these codes work. i just changed the path to relative path like below.
$path = 'sites/default/files/take_snap/20140713110549.jpg';
$del = unlink($path);
Now it deletes file from public sub folder.

CakePHP copy file to new directory

The cakephp documentation is a bit poor according the File api and I would like to use it to copy one file to another directory and if the directory does not exist to create it.
Thanks!
You can do something like this:
$file = new File('your_file.ext');
if ($file->exists()) {
$dir = new Folder('folder_inside_webroot', true);
$file->copy($dir->path . DS . $file->name);
}
API about these classes:
Folder;
File;
I hope it helps.

Resources