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.
|
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.
|
if ($this->driverFile->isExists(...)) { ... } |
More functions are listed below.
1. Check if the file or folder exists on the file system.
|
/** * 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.
|
/** * 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
|
/** * 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 )
|
/** * 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 )
|
/** * 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
|
/** * 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 )
|
/** * 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
|
/** * Returns parent directory's path * * @param string $path * @return string */ public function getParentDirectory($path) |
9. Create a new directory with specific permissions
|
/** * Create directory * * @param string $path * @param int $permissions * @return bool * @throws FileSystemException */ public function createDirectory($path, $permissions = 0777) |
10. Create a new directory recursively
|
/** * 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
|
/** * Read directory * * @param string $path * @return string[] * @throws FileSystemException */ public function readDirectory($path) |
12. Search for a path
|
/** * 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
|
/** * 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
|
/** * 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
|
/** * 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 )
|
/** * Delete file * * @param string $path * @return bool * @throws FileSystemException */ public function deleteFile($path) |
17. Delete a directory recursively
|
/** * Recursive delete directory * * @param string $path * @return bool * @throws FileSystemException */ public function deleteDirectory($path) |
18. Change permissions of a directory
|
/** * 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
|
/** * 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
|
/** * 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
|
/** * 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
|
/** * 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
|
/** * 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.
|
/** * 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
|
/** * 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
|
/** * 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
|
/** * 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
|
/** * 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
|
/** * Close file * * @param resource $resource * @return boolean * @throws FileSystemException */ public function fileClose($resource) |
30. Write to a file like fwrite
|
/** * 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
|
/** * 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.
|
/** * Flushes the output * * @param resource $resource * @return bool * @throws FileSystemException */ public function fileFlush($resource) |
33. Lock a file before write content to a file
|
/** * 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
|
/** * Unlock file * * @param resource $resource * @return bool * @throws FileSystemException */ public function fileUnlock($resource) |
35. Return a absolute path
|
/** * @param string $basePath * @param string $path * @param string|null $scheme * @return string */ public function getAbsolutePath($basePath, $path, $scheme = null) |
36. Fixes a path separator
|
/** * 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
|
/** * Read directory recursively * * @param string $path * @return string[] * @throws FileSystemException */ public function readDirectoryRecursively($path = null) |
38. Return the real path like realpath
|
/** * Get real path * * @param string $path * * @return string|bool */ public function getRealPath($path) |
39. Returns a path for link
|
/** * Return correct path for link * * @param string $path * @return mixed */ public function getRealPathSafety($path) |
Cheers