The framework provides a simple component to work with the filesystem. The component is available in all of the application bundles.
Most of the spiral components rely on the directory registry instead of hard-coded paths.
The registry represented using Spiral\Boot\DirectoriesInterface
.
You can configure application specific directories in the app entry point (app.php):
$app = \App\App::init([
'root' => __DIR__,
'customDir' => __DIR__ . '/custom'
]);
Or using the Bootloader:
namespace App\Bootloader;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Boot\DirectoriesInterface;
class AppBootloader extends Bootloader
{
public function boot(DirectoriesInterface $directories)
{
$directories->set(
'customDir',
$directories->get('root') . '/custom'
);
}
}
To access the directory paths:
namespace App\Controller;
use Spiral\Boot\DirectoriesInterface;
class HomeController
{
public function index(DirectoriesInterface $dirs)
{
dump($dirs->get('root'));
dump($dirs->get('customDir'));
dump($dirs->getAll());
}
}
You can also use the short function
directory
inside your config files. Note, this function does not work outside of the framework as it relies on the global container scope.
Use the Spiral\Files\FilesInterface
component to work with the filesystem:
namespace App\Controller;
use Spiral\Files\FilesInterface;
class HomeController
{
public function index(FilesInterface $files)
{
// get all files from root directory recursively
dump($files->getFiles(directory('root')));
}
}
You can also access this instance using prototyped property files
:
namespace App\Controller;
use Spiral\Prototype\Traits\PrototypeTrait;
class HomeController
{
use PrototypeTrait;
public function index()
{
dump($this->files->exists(__FILE__)); // true
}
}
To ensure that given directory exists use method ensureDirectory
, the second
argument accepts the access permission:
namespace App\Controller;
use Spiral\Boot\DirectoriesInterface;
use Spiral\Files\FilesInterface;
use Spiral\Prototype\Traits\PrototypeTrait;
class HomeController
{
use PrototypeTrait;
public function index(DirectoriesInterface $dirs)
{
$this->files->ensureDirectory(
$dirs->get('customDir'),
FilesInterface::READONLY // or FilesInterface::RUNTIME for editable dirs and files
);
}
}
To check if directory exists:
dump($files->isDirectory(__DIR__));
To delete directory and its content:
$files->deleteDirectory('custom');
To delete directory content only:
$files->deleteDirectory('custom', true);
To check if file exists:
dump($files->exists(__FILE__)); // bool
To get file creation/update time:
dump($files->time(__FILE__)); // unix timestamp
To get file MD5:
dump($files->md5('filename'));
To get filename extension:
dump($files->extension(__FILE__)); // without leading "."
To check if path is file:
dump($files->isFile(__DIR__));
To get file size:
dump($files->size(__DIR__));
To get file/directory permissions:
dump($files->getPermissions(__FILE__)); // int
To set file permissions:
$files->setPermissions(__FILE__, 0777)
Use constants to control file mode:
Constant | Value |
---|---|
FilesInterface::READONLY | 644 |
FilesInterface::RUNTIME | 666 |
To copy file from one path to another:
$files->copy('old-path', 'new-path');
To move file:
$files->move('old-path', 'new-path');
To issue temporary filename:
dump($files->tempFilename());
To issue temporary filename with a specific extension:
dump($files->tempFilename('php'));
To issue temporary filename in a specific location:
dump($files->tempFilename('php', __DIR__));
The component provides multiple methods to operate with file content in an atomic way (without acquiring the file resource):
To write the content to the file (exclusive lock):
$files->write('filename', 'data');
To write/create file and ensure proper access mode:
$files->write('filename', 'data', 0777);
To check and automatically create the file directory:
$files->write('filename', 'data', 0777, true);
Make sure to handle
Spiral\Files\Exception\WriteErrorException
if the file can is not writable.
To append file content:
$files->append('filename', 'data');
To append and ensure file mode:
$files->append('filename', 'data', 0777);
To append/create file and ensure that target directory exists:
$files->append('filename', 'data', 0777, true);
To touch the file and create it if missing:
$files->touch('filename');
To touch file and ensure file mode:
$files->touch('filename', 0777);
To read file content:
dump($files->read('filename'));
Make sure to handle
Spiral\Files\Exception\FileNotFoundException
when files not found.