Magento® 2 offers you a bunch methods for file and folder operations, which allow you to implement quality modules in a short time. Especially if you are planning to publish modules on the Magento Marketplace you have to use those functions, otherwise you won’t meet Magento’s coding standards. In order to have access to cool functions like mkdirRecursive, deleteDirectory or changePermissionsRecursively simply inject \Magento\Framework\Filesystem\Driver\File in your constructor.
1 2 3 4 5 6 7 8 9 10 11 |
protected $driverFile; ... public function __construct( ... \Magento\Framework\Filesystem\Driver\File $driverFile, ... ) { $this->driverFile = $driverFile; ... parent::__construct($context); } |
Once you have re-compiled your code, you should be able to use those functions in your modules like below.
1 2 3 |
if ($this->driverFile->isExists(...)) { ... } |
More functions are listed below.
1. Check if the file or folder exists on the file system.
1 2 3 4 5 6 7 8 |
/** * Is file or directory exist in file system * * @param string $path * @return bool * @throws FileSystemException */ public function isExists($path) |
2. Returns information like ctime and mtime or size, atime, uid and gid.
1 2 3 4 5 6 7 8 |
/** * Gathers the statistics of the given path * * @param string $path * @return array * @throws FileSystemException */ public function stat($path) |
3. Check if a file is readable
1 2 3 4 5 6 7 8 |
/** * Check permissions for reading file or directory * * @param string $path * @return bool * @throws FileSystemException */ public function isReadable($path) |
4. Check if path is a file ( not folder )
1 2 3 4 5 6 7 8 |
/** * Tells whether the filename is a regular file * * @param string $path * @return bool * @throws FileSystemException */ public function isFile($path) |
5. Check if path is a directory ( not a file )
1 2 3 4 5 6 7 8 |
/** * Tells whether the filename is a regular directory * * @param string $path * @return bool * @throws FileSystemException */ public function isDirectory($path) |
6. Get content of a regular file
1 2 3 4 5 6 7 8 9 10 |
/** * Retrieve file contents from given path * * @param string $path * @param string|null $flag * @param resource|null $context * @return string * @throws FileSystemException */ public function fileGetContents($path, $flag = null, $context = null) |
7. Check if a regular file is writable ( not a folder )
1 2 3 4 5 6 7 8 |
/** * Check if given path is writable * * @param string $path * @return bool * @throws FileSystemException */ public function isWritable($path) |
8. Return the parent directory of a path
1 2 3 4 5 6 7 |
/** * Returns parent directory's path * * @param string $path * @return string */ public function getParentDirectory($path) |
9. Create a new directory with specific permissions
1 2 3 4 5 6 7 8 9 |
/** * Create directory * * @param string $path * @param int $permissions * @return bool * @throws FileSystemException */ public function createDirectory($path, $permissions = 0777) |
10. Create a new directory recursively
1 2 3 4 5 6 7 8 9 |
/** * Create a directory recursively taking into account race conditions * * @param string $path * @param int $permissions * @return bool * @throws FileSystemException */ private function mkdirRecursive($path, $permissions = 0777) |
11. Reads a directory and returns an array with all paths of its sub-directories
1 2 3 4 5 6 7 8 |
/** * Read directory * * @param string $path * @return string[] * @throws FileSystemException */ public function readDirectory($path) |
12. Search for a path
1 2 3 4 5 6 7 8 9 |
/** * Search paths by given regex * * @param string $pattern * @param string $path * @return string[] * @throws FileSystemException */ public function search($pattern, $path) |
13. Rename a regular file or a directory
1 2 3 4 5 6 7 8 9 10 |
/** * Renames a file or directory * * @param string $oldPath * @param string $newPath * @param DriverInterface|null $targetDriver * @return bool * @throws FileSystemException */ public function rename($oldPath, $newPath, DriverInterface $targetDriver = null) |
14. Copy a file from A to B
1 2 3 4 5 6 7 8 9 10 |
/** * Copy source into destination * * @param string $source * @param string $destination * @param DriverInterface|null $targetDriver * @return bool * @throws FileSystemException */ public function copy($source, $destination, DriverInterface $targetDriver = null) |
15. Create a symlink
1 2 3 4 5 6 7 8 9 10 |
/** * Create symlink on source and place it into destination * * @param string $source * @param string $destination * @param DriverInterface|null $targetDriver * @return bool * @throws FileSystemException */ public function symlink($source, $destination, DriverInterface $targetDriver = null) |
16. Delete a regular file ( not a file )
1 2 3 4 5 6 7 8 |
/** * Delete file * * @param string $path * @return bool * @throws FileSystemException */ public function deleteFile($path) |
17. Delete a directory recursively
1 2 3 4 5 6 7 8 |
/** * Recursive delete directory * * @param string $path * @return bool * @throws FileSystemException */ public function deleteDirectory($path) |
18. Change permissions of a directory
1 2 3 4 5 6 7 8 9 |
/** * Change permissions of given path * * @param string $path * @param int $permissions * @return bool * @throws FileSystemException */ public function changePermissions($path, $permissions) |
19. Change permissions of a directory and file recursively
1 2 3 4 5 6 7 8 9 10 |
/** * Recursively change permissions of given path * * @param string $path * @param int $dirPermissions * @param int $filePermissions * @return bool * @throws FileSystemException */ public function changePermissionsRecursively($path, $dirPermissions, $filePermissions) |
20. Manipulate the access and modification time of a file
1 2 3 4 5 6 7 8 9 |
/** * Sets access and modification time of file. * * @param string $path * @param int|null $modificationTime * @return bool * @throws FileSystemException */ public function touch($path, $modificationTime = null) |
21. Write content to a file
1 2 3 4 5 6 7 8 9 10 |
/** * Write contents to file in given path * * @param string $path * @param string $content * @param string|null $mode * @return int The number of bytes that were written. * @throws FileSystemException */ public function filePutContents($path, $content, $mode = null) |
22. Opens a file like fopen
1 2 3 4 5 6 7 8 9 |
/** * Open file * * @param string $path * @param string $mode * @return resource file * @throws FileSystemException */ public function fileOpen($path, $mode) |
23. Returns a line from the given handle
1 2 3 4 5 6 7 8 9 10 |
/** * Reads the line content from file pointer (with specified number of bytes from the current position). * * @param resource $resource * @param int $length * @param string $ending [optional] * @return string * @throws FileSystemException */ public function fileReadLine($resource, $length, $ending = null) |
24. Reads a file like fread.
1 2 3 4 5 6 7 8 9 |
/** * Reads the specified number of bytes from the current position. * * @param resource $resource * @param int $length * @return string * @throws FileSystemException */ public function fileRead($resource, $length) |
25. Reads a CSV file like fgetcsv
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * Reads one CSV row from the file * * @param resource $resource * @param int $length [optional] * @param string $delimiter [optional] * @param string $enclosure [optional] * @param string $escape [optional] * @return array|bool|null * @throws FileSystemException */ public function fileGetCsv($resource, $length = 0, $delimiter = ',', $enclosure = '"', $escape = '\\') |
26. Returns position of a pointer like ftell
1 2 3 4 5 6 7 8 |
/** * Returns position of read/write pointer * * @param resource $resource * @return int * @throws FileSystemException */ public function fileTell($resource) |
27. Seeks to a specific offset like fseek
1 2 3 4 5 6 7 8 9 10 |
/** * Seeks to the specified offset * * @param resource $resource * @param int $offset * @param int $whence * @return int * @throws FileSystemException */ public function fileSeek($resource, $offset, $whence = SEEK_SET) |
28. Returns true if pointer is at the end of a file
1 2 3 4 5 6 7 |
/** * Returns true if pointer at the end of file or in case of exception * * @param resource $resource * @return boolean */ public function endOfFile($resource) |
29. Close a file
1 2 3 4 5 6 7 8 |
/** * Close file * * @param resource $resource * @return boolean * @throws FileSystemException */ public function fileClose($resource) |
30. Write to a file like fwrite
1 2 3 4 5 6 7 8 9 |
/** * Writes data to file * * @param resource $resource * @param string $data * @return int * @throws FileSystemException */ public function fileWrite($resource, $data) |
31. Write one row to a CSV file
1 2 3 4 5 6 7 8 9 10 11 |
/** * Writes one CSV row to the file. * * @param resource $resource * @param array $data * @param string $delimiter * @param string $enclosure * @return int * @throws FileSystemException */ public function filePutCsv($resource, array $data, $delimiter = ',', $enclosure = '"') |
32. Flush output of a file. See fflush.
1 2 3 4 5 6 7 8 |
/** * Flushes the output * * @param resource $resource * @return bool * @throws FileSystemException */ public function fileFlush($resource) |
33. Lock a file before write content to a file
1 2 3 4 5 6 7 8 9 |
/** * Lock file in selected mode * * @param resource $resource * @param int $lockMode * @return bool * @throws FileSystemException */ public function fileLock($resource, $lockMode = LOCK_EX) |
34. Unlock a file after all file manipulations are done
1 2 3 4 5 6 7 8 |
/** * Unlock file * * @param resource $resource * @return bool * @throws FileSystemException */ public function fileUnlock($resource) |
35. Return a absolute path
1 2 3 4 5 6 7 |
/** * @param string $basePath * @param string $path * @param string|null $scheme * @return string */ public function getAbsolutePath($basePath, $path, $scheme = null) |
36. Fixes a path separator
1 2 3 4 5 6 7 8 |
/** * Fixes path separator * Utility method. * * @param string $path * @return string */ protected function fixSeparator($path) |
37. Reads a directory recursively and returns paths as an array
1 2 3 4 5 6 7 8 |
/** * Read directory recursively * * @param string $path * @return string[] * @throws FileSystemException */ public function readDirectoryRecursively($path = null) |
38. Return the real path like realpath
1 2 3 4 5 6 7 8 |
/** * Get real path * * @param string $path * * @return string|bool */ public function getRealPath($path) |
39. Returns a path for link
1 2 3 4 5 6 7 |
/** * Return correct path for link * * @param string $path * @return mixed */ public function getRealPathSafety($path) |
Cheers