Files are managed in a private directory outside of the web root. This ensures that the application can prevent unauthorized access to files uploaded by the user or generated by the system.
The files directory is specified in the files_dir
setting in the configuraton file.
The File Service is a helper class to read and write files in this directory. When a file is added or deleted, it will add or remove a row from the files
table in the database.
Add a file.
use APP\core\Services;
$fileId = Services::get('file')->add('/absolute/path/to/source.txt', 'destination.txt');
Delete a file.
use APP\core\Services;
Services::get('file')->delete($fileId);
Get the relative path to a file in the file directory.
use APP\core\Services;
$file = Services::get('file')->get($fileId);
$path = $file->path;
Download a file in the file directory.
use APP\core\Services;
$downloadFileAs = 'my-special-file.txt';
Services::get('file')->download($file->path, $downloadFileAs);
Generate a safe filename from any string.
use APP\core\Services;
$title = $publication->getLocalizedFullTitle();
$file = Services::get('file')->get($fileId);
$filename = Services::get('file')->formatFilename($file->path, $title);
Get the mimetype of a file.
use APP\core\Services;
$file = Services::get('file')->get($fileId);
$mimetype = $file->mimetype;
File relationships in the database should always refer to the file_id
in the files
table. Use a foreign key when possible.
The File Service is a small wrapper around Flysystem. Access the library directly to use the Filesystem API.
Check if a file exists.
use APP\core\Services;
$file = Services::get('file')->get($fileId);
if (Services::get('file')->fs->has($file->path)) {
...
}
Get the size of a file.
use APP\core\Services;
$file = Services::get('file')->get($fileId);
$size = Services::get('file')->fs->getSize($file->path);
By default, the File Service instantiates the local file system adaptor and configures it to use the files_dir
directory. Adaptors are available for other file systems such as AWS and Azure. Use the Flysystem API, not PHP’s file functions, in order to ensure operations are compatible with all of the filesystem adaptors.
Files for a journal, press or preprint server are stored in distinct subdirectories. Get the path to the context directory to add a file to a context.
use APP\core\Application;
use APP\core\Services;
$context = Application::get()->getRequest()->getContext();
$appFileDirectories = Application::get()->getFileDirectories();
$path = $appFileDirectories['context'] . $context->getId();
$fileId = Services::get('file')->add('source.txt', $path . '/destination.txt');
Files for a submission are stored in distinct subdirectories. Use the submission file repository to get a path to a submission’s directory.
use APP\core\Services;
use APP\facades\Repo;
$path = Repo::submissionFile()->getSubmissionDir($contextId, $submissionId);
$fileId = Services::get('file')->add('source.txt', $path . '/destination.txt');
Read more about working with Submission Files.
The public files directory is deprecated and should not be used for new features.
Public files are accessible to anyone and may include files uploaded to a user signature or bio. These files are stored in a public directory inside the web root, so that they can be accessed directly by HTTP request.
Use the PublicFileManager
to read and write public files.
Temporary files are used to stage changes before they are applied. For example, temporary files are used to upload a new journal logo, but the logo will not be changed until the form is saved.
Learn how to use temporary files in the REST API Guide.