This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

Adding Files to the File Manager

There is one unified method for adding files to the concrete5 file manager, which the various methods in the file manager (upload single, upload multiple, transfer remote and transfer from incoming) all employ: the FileImporter class.

Importing the File

The following code adds a file to the concrete5 file manager:

   Loader::library("file/importer");      $fi = new FileImporter();      $newFile = $fi->import($pathToFile, $nameOfFile, $fileObject); 

Arguments

  • $pathToFile - the path to the file on the local web server.
  • An optional $nameOfFile parameter. - the filename that you want this file to be named as. This can be anything. If it doesn't match the current filename referenced in $pathToFile, the file will be renamed to this new name. If it is left blank, the current filename will be used.
  • An optional $fileObject parameter. This will likely be blank, but if it is specified, the file referenced within the current version of the $fileObject will be replaced by the newly imported file.
  • The FileImporter::import() call returns a FileVersion object for the new file in the system. FileImporter::import() takes care of storing the file, inserting the file's information into the system, setting up any initial file attributes, running any custom metadata routines, and building any thumbnails.

Checking Permissions

The concrete5 file manager automatically checks to see whether the current logged-in user has the ability to upload files, and if so, whether the type of the file uploaded matches types they are allowed to upload. It is wise to enable this similar functionality in scripts that employ FileImporter::import() as well.

The following code ensures that the logged-in user has the ability to upload files, and that the type of file uploaded matches their permissions:

$cf = Loader::helper('file');  $fp = FilePermissions::getGlobal();  if (!$fp->canAddFiles()) {  die('Unable to add files');  }  if (!$fp->canAddFileType($cf->getExtension($pathToFile))) {  return FileImporter::E_FILE_INVALID_EXTENSION;  }

Here we check two permissions. The first call checks the general "add file" permissions. If the user is not allowed to upload files at all, the script will die with the "Unable to add files" message. The second call uses the built-in concrete5 file helper to grab the extension of the file being imported, and check it against the user's file permissions. If the user doesn't have the ability to upload files of this type, we return the FileImporter::E_FILE_INVALID_EXTENSION error code.

Error Handling

In the previous permissions example, we reference a file error code. Here is a list of all possible error codes available from the FileImporter class:

FileImporter::E_PHP_FILE_ERROR_DEFAULT  FileImporter::E_PHP_FILE_EXCEEDS_UPLOAD_MAX_FILESIZE  FileImporter::E_PHP_FILE_EXCEEDS_HTML_MAX_FILE_SIZE  FileImporter::E_PHP_FILE_PARTIAL_UPLOAD  FileImporter::E_PHP_NO_FILE  FileImporter::E_FILE_INVALID_EXTENSION  FileImporter::E_FILE_INVALID  FileImporter::E_FILE_UNABLE_TO_STORE

If your script or the core concrete5 importing routine returns any of these error codes, they can be mapped to readable error strings using this function:

print FileImporter::getErrorMessage($errorCode);

If the FileImporter::import() function doesn't return a FileVersion object, the number it does return may be passed to the static getErrorMessage function, which will explain where the operation failed.

Loading Conversation