Useful Core Services for Working with Files

Improvements?

Let us know by posting here.

There are a number of useful core services available for working with files. Typically these don't involve Concrete CMS – they're general purpose helper methods.

Getting the Service

You can get access to the general file service this way:

$service = app('helper/file');
 note: \Core::make() is deprecated . Use app() (since 8.5.2) or \Concrete\Core\Support\Facade\Application::getFacadeApplication() . See this tutorial for more details.

This returns an instance of the \Concrete\Core\File\Service\File object.

Useful Methods

Here are some things you can do with this service.

Retrieve a list of files in a Directory

$files = $service->getDirectoryContents('/absolute/path/to/directory');

Recursively copy all items in a source directory to a target directory

$service->copyAll('/absolute/path/to/directory', '/target/directory');

Remove all files from a directory

$service->removeAll('/absolute/path/to/directory');

Force the Download of a File

$service->forceDownload('/path/to/my/file.zip');

Get the Temporary Directory

$temporaryDirectory = $service->getTemporaryDirectory();

Get a file's extension

$extension = $service->getExtension($myFilename);

Compare Paths

Checks if two path are the same, considering directory separator and OS case sensitivity.

$isSame = $service->isSamePath($path1, $path2);

API Reference

There are additional methods available in this class. These are detailed in the API documentation:

File Service API Reference