How to create folder base on users input using cakephp3 - cakephp

$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

Related

Rename an image with laravel 5

I have a directory with images and I show to the user,the user can rename a image, I have the file with
$images = File::files('img');
$image = $images[0];
But I don't know how rename an image.
Thanks
You can retrieve it first from the input. and move it using file name you define.
Your image will using the file name you specify.
$picture= $request->file('image');
$fileName = 'what the image you want to call';
$picture->move('image/foo', $fileName);
Brother, This is work for me. And I hope, It'll serve your purpose
$destinationPath = "uploadedImages";
$newName = md5_file($request->file('file_name')->getRealPath());
$guessFileExtension = $request->file('file_name')->guessExtension();
$file = $request->file('file_name')->move($destinationPath, $newName.'.'.$guessFileExtension);

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.

how to check if folder exists 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();

zend framework multiple input file upload

i have a form that contains two file element with unique name. i want to reach each file element from Zend_File_Transfer_Adapter_Http(); how can i do that?
if i use
$apt = new Zend_File_Transfer_Adapter_Http();
.
.
$apt->receive();
two files have been uploaded. but i execute different queries for each file element.
for example
$smallPic = small pic name that i get from input smallPic
i execute
update products set smallPic = '$smallPic'
and for large pic
$largePic = large pic name that i get from input largePic
i execute
update products set largePic = '$largePic'
how can i reach each input file element with $apt ?
Here is some code I have used in the past to receive multiple files from a Zend_Form where many of the file upload fields were dynamic and not part of the form.
Since you know the names of the two file uploads, you can also say $apt->getFileInfo('my_file');
$apt = new Zend_File_Transfer_Adapter_Http();
$files = $apt->getFileInfo();
foreach($files as $file => $fileInfo) {
if ($apt->isUploaded($file)) {
if ($apt->isValid($file)) {
if ($apt->receive($file)) {
$info = $apt->getFileInfo($file);
$tmp = $info[$file]['tmp_name'];
$data = file_get_contents($tmp);
// here $tmp is the location of the uploaded file on the server
// var_dump($info); to see all the fields you can use
}
}
}
}
Hope that helps.

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