3.4 3.3
Jump to table of contents

Files

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.

File Service #

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.

$fileId = Services::get('file')->add('/absolute/path/to/source.txt', 'destination.txt');

Delete a file.

Services::get('file')->delete($fileId);

Get the relative path to a file in the file directory.

$file = Services::get('file')->get($fileId);
$path = $file->path;

Download a file in the file directory.

$downloadFileAs = 'my-special-file.txt';
Services::get('file')->download($file->path, $downloadFileAs);

Generate a safe filename from any string.

$title = $publication->getLocalizedFullTitle();
$file = Services::get('file')->get($fileId);
$filename = Services::get('file')->formatFilename($file->path, $title);

Get the mimetype of a file.

$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.

Filesystem #

The File Service is a small wrapper around Flysystem 1.x. Access the library directly to use the Filesystem API.

Check if a file exists.

$file = Services::get('file')->get($fileId);
if (Services::get('file')->fs->has($file->path)) {
	...
}

Get the size of a file.

$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.

Context Files #

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.

$context = $request->getContext();
$appFileDirectories = \Application::get()->getFileDirectories();
$path = $appFileDirectories['context'] . $context->getId();
$fileId = Services::get('file')->add('source.txt', $path . '/destination.txt');

Submission Files #

Files for a submission are stored in distinct subdirectories. Use the Submission File Service to get a path to a submission’s directory.

$path = Services::get('submissionFile')->getSubmissionDir($contextId, $submissionId);
$fileId = Services::get('file')->add('source.txt', $path . '/destination.txt');

Read more about working with Submission Files.

Public 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 #

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.