Let the platform do the work

Working with File Uploads

Overview

The UploadFile class handles the various tasks when uploading a file.

Retrieving a Files Upload Location

To retrieve a files upload path, you can use the get_upload_path method and pass in the file's GUID id.

  require_once 'include/upload_file.php';
UploadFile::get_upload_path($file_id);

This method will normally return the path as:

  upload://1d0fd9cc-02e5-f6cd-1426-51a509a63334

Retrieving a Files Full File System Location

To retrieve a files full system path, you can use the get_upload_path and real_path methods as shown below:

  require_once 'include/upload_file.php';
UploadFile::realpath(UploadFile::get_upload_path($file_id));

This method will normally return the path where they are uploaded to. Each uploaded file is now stored in a subdirectory derived from the UUID filename. Existing files will be moved into subdirectories during upgrade. For example, the file 3657325a-bdd6-11eb-9a6c-08002723a3b8 will now be stored in the ./upload/25a/ subdirectory. These UID character locations were selected to ensure even distribution of files across the new subdirectories.

Retrieving a Files Contents

As an alternative to using file_get_contents or sugar_file_get_contents, you can retrieve the contents of a file using the get_file_contents method as shown below:

  require_once 'include/upload_file.php';

$file = new UploadFile();

//get the file location
$file->temp_file_location = UploadFile::get_upload_path($file_id);
$file_contents = $file->get_file_contents();

Duplicating a File

To duplicate an uploaded file, you can use the duplicate_file method by passing in the files current id and the id you would like it copied to as shown below:

  require_once 'include/upload_file.php';

$uploadFile = new UploadFile();
$result = $uploadFile->duplicate_file($oldFileId, $newFileId);

Topics