I am having troubles with Symfony Filesystem class. I am trying to delete a folder via remove method.
This is my method.
public function createArchive($source, $destination) {
$zipper = new Zipper();
$zipper->make('backup/'.$this->getBackupName().'.zip')->add('backup/'.$this->getBackupName().'/');
$fileSystem = new Filesystem();
$fileSystem->remove('backup/'.$this->getBackupName());
}
Zipper creates zip archive with folder i put in second argument. So for example i have a folder backup/2016_03_24_full it will create zip archive in backup/2016_03_24_full.zip.
Then i am trying to remove the backup/2016_03_24_full folder so only zip archives are there. But Filesystem remove method is deleting also my zip. It should delete only folder i put in argument, but something is going wrong..
What could be the problem?
Since you're passing a folder to the remove method, it first tries to delete the directory by calling rmdir(). This function expects the folder to be empty. Since your directory is not empty, a fallback comes into play which relies on PHP's FilesystemIterator.
My guess is that giving the name of the folder without the trailing slash (/) is picking the files from outside and inside the folder both and removing them.
You can see the concrete algorithm here.
Related
I'm trying to create and store some HTML code from a textarea into a file
fopen method in PHP will create a file if that file doesn't exist already but it is not working in Laravel 5.7
Here is my demo code
$plugin_html_file_nm = "plugin_html".time().".html";
$html_plugin = fopen("/public/plugin_html_storage/".$plugin_html_file_nm,"w");
fwrite($html_plugin,$request->input('plugin_html'));
$plugin->plugin_html = $plugin_html_file_nm;
This error is given fopen(/public/plugin_html_storage/plugin_html1546535810.html): failed to open stream: No such file or directory
Can anyone help me?
Laravel provides a helper public_path()
The public_path function returns the fully qualified path to the
public directory. You may also use the public_path function to
generate a fully qualified path to a given file within the public
directory
Maybe you need to add the full path. So:
$html_plugin = fopen(public_path("plugin_html_storage/.$plugin_html_file_nm"),"w");
Also, make sure you have writing privilegies. As the PHP documentation states:
The file must be accessible to PHP, so you need to ensure that the file access permissions allow this access.
Your laravel app by default is served from the public directory.
So in order to achieve what you're trying to do, replace
$html_plugin = fopen("/public/plugin_html_storage/".$plugin_html_file_nm,"w");
with:
$html_plugin = fopen("./plugin_html_storage/".$plugin_html_file_nm, "w");
Note: The plugin_html_storage directory should be created in the public directory so the operation doesn't fail.
You just need to add the relative path.
$html_plugin = fopen("../public/plugin_html_storage/".$plugin_html_file_nm,"w");
file_put_contents(public_path()."plugin_html".time().".html",
$request->input('plugin_html'));
Is there a way to delete all files in specific directory. I'm trying to clear all my files in my created folder backgrounds in storage\app\backgrounds but in docs seems no method for delete all.
Storage::delete('backgrounds\*.jpg');
I don't think if this is the best way to solve this. But I solved mine calling
use Illuminate\Filesystem\Filesystem;
Then initiate new instance
$file = new Filesystem;
$file->cleanDirectory('storage/app/backgrounds');
for Laravel >= 5.8
use Illuminate\Support\Facades\Storage;
// Get all files in a directory
$files = Storage::allFiles($dir);
// Delete Files
Storage::delete($files);
Just use it.
File::cleanDirectory($direction);
You can use Filesystem method cleanDirectory
$success = Storage::cleanDirectory($directory);
Please see documentation for more information:
https://laravel.com/api/5.5/Illuminate/Filesystem/Filesystem.html#method_cleanDirectory
In Laravel 5.8 you can use:
Storage::deleteDirectory('backgrounds');
Remember to include:
use Illuminate\Support\Facades\Storage;
In Laravel 5.7 you can empty a directory using the Storage facade like so:
Storage::delete(Storage::files('backgrounds'));
$dirs = Storage::directories('backgrounds');
foreach ($dirs as $dir) {
Storage::deleteDirectory($dir);
}
The delete() method can receive an array of files to delete, while deleteDirectory() deletes one directory (and its contents) at a time.
I don't think it's a good idea to delete and then re-create the directory as that can lead to unwanted race conditions.
I'm handling this by deleting the whole directory as I don't need it. But if, for any case, you need the directory you should be good by just recreating it:
$d = '/myDirectory'
Storage::deleteDirectory($d);
Storage::makeDirectory($d);
//You can use Illuminate\Filesystem\Filesystem and it's method cleanDirectory('path_to_directory).
For Example:
$FolderToDelete = base_path('path_to_your_directory');
$fs = new \Illuminate\Filesystem\Filesystem;
$fs->cleanDirectory($FolderToDelete);
//For Delete All Files From Given Directory.
$succes = rmdir($FolderToDelete);
//For Delete Directory
//This Method Works for me
#Laravel
#FileManager
#CleanDirectory
I wrote custom classes and want to use them in pimcore application.
I took them to /website/lib/Custom directory on server. Afterwards, I wrote recursive script includer for each Class located in the directory and included that script in /index.php file.
It is absolutely not pimcore standard but it works.
In pimcore/config/startup.php exists snippet:
$autoloaderClassMapFiles = [
PIMCORE_CONFIGURATION_DIRECTORY . "/autoload-classmap.php",
PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY . "/autoload-classmap.php",
PIMCORE_PATH . "/config/autoload-classmap.php",
];
$test = PIMCORE_ASSET_DIRECTORY;
foreach ($autoloaderClassMapFiles as $autoloaderClassMapFile) {
if (file_exists($autoloaderClassMapFile)) {
$classMapAutoLoader = new \Pimcore\Loader\ClassMapAutoloader([$autoloaderClassMapFile]);
$classMapAutoLoader->register();
break;
}
}
I guess that this provides inclusion of all those classes put into returning array from autoload-classmap.php.
Having in mind that /pimcore/config/autoload-classmap.php exists, the mentioned loop would break at first iteration so classes that I would put into custom autoload-classmap are not going to be included in project.
My question is can I change files from /pimcore directory and expect that everything would be fine after system update?
No, you should not overwrite anything in the pimcore directory, since the files in there get overwritten by the update mechanism.
You can do what you want by using the /website/config/startup.php which will not get overwritten:
https://www.pimcore.org/wiki/display/PIMCORE4/Hook+into+the+startup-process
But instead of loading all your classes as you did, take advantage of the autoloader by adding this to the /website/config/startup.php:
// The first line is not absolutely necessary, since the $autoloader variable already gets
// set in the /pimcore/config/startup.php, but it is a more future-proof option
$autoloader = \Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Custom');
If you are properly using namespaces and naming your files correctly that's all you need to do.
I want to loop all the sub dirs in the main dirs, where I keeps all my classes, for instance,
core/
model/
page/
class_1.php
class_2.php
menu/
class_3.php
and so on...
So this is my autoload function that I place it in the init.php,
function autoload_multiple_directory($class_name){
// List all the class directories in the array.
$array_directories = array(
'core/controller/',
'core/model/',
'core/helper/'
);
// When you use namespace in a class, you get something like this when you auto load that class \foo\tidy.
// So use explode to split the string and then get the last item in the exloded array.
$parts = explode('\\', $class_name);
//print_r($parts);
// Set the class file name.
$file_name = strtolower(end($parts)).'.php';
// $file_name = 'class_'.strtolower($class_name).'.php';
// Loop the array.
foreach($array_directories as $path_directory){
$recursive_directory = new RecursiveDirectoryIterator($path_directory);
foreach (new RecursiveIteratorIterator($recursive_directory) as $filename => $file) {
if(file_exists(WEBSITE_DOCROOT.$file->getPath().'/'.$file_name)){
include WEBSITE_DOCROOT.$file->getPath().'/'.$file_name;
}
}
/* no problem with this, but I cannot loop the sub dirs...
if(file_exists(WEBSITE_DOCROOT.$path_directory.$file_name)){
include WEBSITE_DOCROOT.$path_directory.$file_name;
}
*
*/
}
}
spl_autoload_register('autoload_multiple_directory');
But then I get this error message,
Fatal error: Cannot redeclare class Common in C:\wamp\www\xxx\core\helper\Common.php on line 6
There is only one Common class in my project. Why does it say more than once or redeclare?
But if you look at the if(file_exists(WEBSITE_DOCROOT.$path_directory.$file_name)) that I comment out - it has no problem to load the classes. The problem of this initial loop is that it does not loop the sub dirs in a main dir, for instance, core/model/
Any ideas why and what should I do to loop the sub dirs of a main dir?
EDIT:
The problem comes from RecursiveDirectoryIterator - it loops the directories and lists all files. But what I want is only the sub directories.
Is there any chance that there are more than one copy of Common.php file exists in those folders?
Because your code does not break after including a class file the autoloader will continue seeking other files with the same name in the folder tree, and would lead to Fatal error: Cannot redeclare class XXX error. Adding break could fix the problem.
// Loop the array.
$isClassFound = false;
foreach($array_directories as $path_directory){
$recursive_directory = new RecursiveDirectoryIterator($path_directory);
foreach (new RecursiveIteratorIterator($recursive_directory) as $filename => $file) {
if(file_exists(WEBSITE_DOCROOT.$file->getPath().'/'.$file_name)){
include WEBSITE_DOCROOT.$file->getPath().'/'.$file_name;
$isClassFound = true;
}
if ($isClassFound) break;
}
if ($isClassFound) break;
}
However, the nature of your autoloader seems like it should not allow any duplicated class file names. Maybe you can write a name duplication checker to guarantee uniqueness.
EDIT:
I removed the class_exists() part from my answer because using it doesn't make much sense. Anyway, since you saw that version of my answer, and you asked me where to put class_exists() via the comment, I'll revive the code sample. you can add the following code at the beginning of the autoloader.
if (class_exists($class_name,false)) // give false to avoid automatic loading
return;
Open the start menu. In the text box write cmd, wait for the cmd program to pop up and then hit Enter.
Once the terminal window opens navigate to your root folder and then do a recursive search (through all files and folders) for the class' name:
cd C:\wamp\www\xxx
findstr /SNIP /C:"class common" *.php
There should be more than one declaration of the class.
I don't understand Gaufrette and symfony2.
This seems to me like it's only working for textfiles/textcontent.
I can create a file but can't copy from a local source (i.e. a path).
What i would like to do is something like this:
$adapter = new LocalAdapter($realpath);
$filesystem = new Filesystem($adapter);
$filesystem->fromUploadedFile($tempPathOfUploadedFile,$idForGaufrette);
How do I store an image and how do I handle it's output when requested by the user?
Update:
How can I access the temp filename of an uploaded file in symfony"?
How can I access the existing, private attribute $path that exists in the Symfony\Component\HttpFoundation\File\UploadedFile Object in Symfony2?
The method copy(x, y) is not implemented but if you want storing a file you can move it using the method
rename($key, $new)
defined in the Filesystem Class.
For handle the output when requested by the user, all you need is a link to the image path (probaly in the database). So you don't need the filesystem (you can check if the file exists with the has($key) method).
In all cases use the adapter Local to work in local.
If you need a stream wrapper like "ftp://mydomain/myPicture" i recently send a PR to configures the stream wrapper in the config.yml, and register filiesystems that you want and their domain.
To get the tmp filename:
$file->getPathName(); // /tmp/filename
where $file is a Symfony\Component\HttpFoundation\File\UploadedFile Object.