On this article, we will discover the Symfony Filesystem part, which offers helpful strategies to work together with a file system. After set up and configuration, we’ll create just a few real-world examples of how you can use it.
The Symfony Filesystem Part
As a rule, you may have to work together with a file system for those who’re coping with PHP functions. Normally, you both find yourself utilizing the core PHP capabilities or create your personal customized wrapper class to attain the specified performance. Both method, it is tough to keep up over an extended time frame. So what you want is a library which is properly maintained and straightforward to make use of. That is the place the Symfony Filesystem part is available in.
The Symfony Filesystem part offers helpful wrapper strategies that make the file system interplay a breeze and a enjoyable expertise. Let’s rapidly have a look at what it is able to:
- making a listing
- making a file
- modifying file contents
- altering the proprietor and group of a file or listing
- making a symlink
- copying a file or listing
- eradicating a file or listing
- and extra
On this article, I am going to present you how you can unleash the ability of the Symfony Filesystem part. As normal, we’ll begin with set up and configuration directions, after which we’ll implement just a few real-world examples to show the important thing ideas.
Set up and Configuration
On this part, we will set up the Symfony Filesystem part. I assume that you have already put in Composer in your system as we’ll want it to put in the Filesystem part obtainable at Packagist.
So go forward and set up the Filesystem part utilizing the next command.
$composer require symfony/filesystem
That ought to have created a composer.json file, which ought to appear like this:
{ "require": { "symfony/filesystem": "^4.1" } }
So that is the set up half, however how are you supposed to make use of it? In truth, it is only a matter of together with the autoload.php file created by Composer in your software, as proven within the following snippet.
<?php require_once './vendor/autoload.php'; // software code ?>
A Actual-World Instance
On this part, we’ll create an instance which demonstrates how you can use the Filesystem part in your functions to carry out numerous filesystem operations.
To begin with, let’s go forward and create the index.php file with the next contents.
<?php require_once './vendor/autoload.php'; use SymfonyComponentFilesystemFilesystem; use SymfonyComponentFilesystemExceptionIOExceptionInterface; // init file system $fsObject = new Filesystem(); $current_dir_path = getcwd(); // make a brand new listing // create a brand new file and add contents // copy a listing // take away a listing
Right here, we have initialized the Filesystem
object to $fsObject
and saved the present listing to $current_dir_path
. Within the upcoming sections, we’ll use $fsObject
to carry out totally different operations.
Make a New Listing
First, we’ll create a brand new listing.
//make a brand new listing strive { $new_dir_path = $current_dir_path . "/foo"; if (!$fsObject->exists($new_dir_path)) { $previous = umask(0); $fsObject->mkdir($new_dir_path, 0775); $fsObject->chown($new_dir_path, "www-data"); $fsObject->chgrp($new_dir_path, "www-data"); umask($previous); } } catch (IOExceptionInterface $exception) { echo "Error creating listing at". $exception->getPath(); }
Right here, we have used the exists
technique to test if the foo listing already exists earlier than creating it.
Subsequent, we used the mkdir
technique to create the foo listing with the 0775 permissions, which implies readable and executable by all, however solely writable by the file proprietor and their group. (That is the octal notation for filesystem permissions—to study extra, take a look at this breakdown of octal notation.) Additional, we have used the chown and chgrp strategies to alter the proprietor and group of the foo listing.
Create a New File and Add Contents
On this part, we’ll create a brand new file and add contents to that file.
// create a brand new file and add contents strive { $new_file_path = $current_dir_path . "/foo/bar.txt"; if (!$fsObject->exists($new_file_path)) { $fsObject->contact($new_file_path); $fsObject->chmod($new_file_path, 0777); $fsObject->dumpFile($new_file_path, "Including dummy content material to bar.txt file.n"); $fsObject->appendToFile($new_file_path, "This must be added to the top of the file.n"); } } catch (IOExceptionInterface $exception) { echo "Error creating file at". $exception->getPath(); }
Right here, we have used the contact
technique to create a brand new file after which used chmod
to set its permissions to 0777—globally readable, writable, and executable.
As soon as the file is created, you need to use the dumpFile
technique so as to add contents in that file. Then again, if you wish to add contents to the already current file, you need to use the appendToFile
technique, as proven within the above instance.
Copy a Listing
Thus far, we have created the foo listing and the bar.txt file utilizing the $fsObject
object. On this part, we’ll see how you can copy a listing together with the contents.
//copy a listing strive { $src_dir_path = $current_dir_path . "/foo"; $dest_dir_path = $current_dir_path . "/foo_copy"; if (!$fsObject->exists($dest_dir_path)) { $fsObject->mirror($src_dir_path, $dest_dir_path); } } catch (IOExceptionInterface $exception) { echo "Error copying listing at". $exception->getPath(); }
As you’ll be able to see, first we constructed the trail names with string concatenation. Then, as soon as we made positive the listing did not exist already utilizing the exists
technique, we used the mirror
technique to repeat the foo listing into the foo_copy listing.
Take away a Listing
Lastly, let’s have a look at how you can take away a listing.
//take away a listing strive { $arr_dirs = array( $current_dir_path . "/foo", $current_dir_path . "/foo_copy" ); $fsObject->take away($arr_dirs); } catch (IOExceptionInterface $exception) { echo "Error deleting listing at". $exception->getPath(); }
Once more, it is fairly simple—to delete a listing, you simply use the take away
technique.
You could find the full code to index.php in our GitHub repo.
Conclusion
In order that’s a short introduction to the Symfony Filesystem part. The Symfony Filesystem part offers strategies that make interplay with a file system a breeze. We checked out how you can set up the part, and we created a handful of examples to show numerous facets of the part.
I hope that you have loved this text, and be at liberty to publish your ideas utilizing the feed beneath!