File Processing Trait

The FileProcessingTrait in the LaraUtilX package provides functionality for handling file operations such as uploading, downloading, and deleting files using Laravel's Storage facade.

Methods

  1. getFile(string $filename, string $directory = 'uploads'): ?string: Retrieves file contents, or null when the file does not exist.
  2. uploadFile(UploadedFile $file, string $directory = 'uploads'): string: Uploads a single file and returns its stored name.
  3. uploadFiles(array $files, string $directory = 'uploads'): array: Uploads multiple files.
  4. deleteFile(string $filename, string $directory = 'uploads'): Deletes a single file.
  5. deleteFiles(array $filenames, string $directory = 'uploads'): Deletes multiple files.

Usage

  1. Get File Contents:

    use LaraUtilX\Traits\FileProcessingTrait;
    
    class MyController extends Controller
    {
       use FileProcessingTrait;
    
       public function getFileContents(string $filename)
       {
           // Returns null when the file is missing, so check explicitly
           // rather than comparing against a sentinel string.
           $contents = $this->getFile($filename);
    
           if ($contents === null) {
               abort(404);
           }
    
           return $contents;
       }
    }
  2. Upload File:

    use LaraUtilX\Traits\FileProcessingTrait;
    
    class MyController extends Controller
    {
       use FileProcessingTrait;
    
       public function uploadSingleFile(Request $request)
       {
           $file = $request->file('file');
    
           return $this->uploadFile($file);
       }
    }
  3. Upload Multiple Files:

    use LaraUtilX\Traits\FileProcessingTrait;
    
    class MyController extends Controller
    {
       use FileProcessingTrait;
    
       public function uploadMultipleFiles(Request $request)
       {
           $files = $request->file('files');
    
           return $this->uploadFiles($files);
       }
    }
  4. Delete File:

    use LaraUtilX\Traits\FileProcessingTrait;
    
    class MyController extends Controller
    {
       use FileProcessingTrait;
    
       public function deleteSingleFile(string $filename)
       {
           $this->deleteFile($filename);
       }
    }
  5. Delete Multiple Files:

    use LaraUtilX\Traits\FileProcessingTrait;
    
    class MyController extends Controller
    {
       use FileProcessingTrait;
    
       public function deleteMultipleFiles(array $filenames)
       {
           $this->deleteFiles($filenames);
       }
    }

Result

The trait provides convenient methods for performing file operations, simplifying file management tasks in Laravel applications.

  • Success Response: File operations such as uploading, downloading, and deleting are executed successfully, and appropriate responses or actions are taken based on the operation.
  • Error Response: If an error occurs during file operations (e.g., file not found, permission denied), appropriate error handling mechanisms should be implemented based on application requirements.

Stored filenames

uploadFile() returns the name the file was stored under, which is random and carries only the original extension:

hK3nQ1p8ZvR7yTgWc2LmXbF4dJ6sNaE9uY0iOoPq.pdf

The client-supplied filename is never reused, so a hostile name cannot influence where the file lands or what it is called on disk. Keep the returned value if you need to retrieve or delete the file later, and store the original name separately when you want to show it back to the user.

Changed in v1.5.4. Stored names were previously uniqid() prefixed onto the client's original filename, and getFile() returned the literal string "File not found" for a missing file, which was indistinguishable from a file whose contents said so.

Publish

The trait is available as soon as the package is installed; there is no publish step. Use it directly from the package namespace:

use LaraUtilX\Traits\FileProcessingTrait;



This trait streamlines file handling in Laravel applications, enhancing productivity and maintainability.