Skip to content

Leaf Docs Show Incorrect Error Handling for Files #139

Description

@emajekral-collab

Hi again,

On this page: https://leafphp.dev/docs/utils/fs.html we see error reporting for file upload as:

$uploaded = request()->upload('fileToUpload', 'path/to/uploads');

if ($uploaded) {
  echo 'File uploaded successfully';
} else {
  $errors = request()->errors();
}

However, in Leaf v. 4.4 request()->upload() does file relocation with \Leaf\FS\File::upload() which logs errors to its own static::errorsArray. This errors array is not merged by request()->errors(). The code in \Leaf\Http::errors() shows:

    public static function errors()
    {
        return array_merge(
            static::$errors,
            static::$validator ? static::$validator->errors() : [],
            class_exists('\Leaf\Auth') ? static::auth()->errors() : []
        );
    }

This change fixes the issue for me:

    public static function errors()
    {
        return array_merge(
            static::$errors,
            static::$validator ? static::$validator->errors() : [],
            class_exists('\Leaf\Auth') ? static::auth()->errors() : [],
            \Leaf\FS\File::errors() // merges File system errors to handle errors from upload.
        );
    }

I'm having students merge the two errors arrays manually in their code like this:

$uploadOk = request()->upload(/*details, yadda*/);
if (!$uploadOk)
{
   $errors = array_merge($errors, request()->errors(), \Leaf\FS\File::errors()); // I'm having them add their own errors in the Controller as well for feedback in the View

It would be nice if either they didn't have to check the filesystem when they didn't appear to call it, or if the docs reflected the actual code. My test code from last January shows that I was doing the merge then, but I don't recall noticing it at the time. Possibly the documentation was incorrectly updated since then?

Thanks for your attention to this!

Activity

  1. emajekral-collab commented on Jun 23, 2026

    @emajekral-collab
    Author

    While on the subject of that page, the sample upload request:

    $uploaded = request()->upload('fileToUpload', 'path/to/uploads', [
      'maxSize' => 1024 * 1024, // 1MB
      'allowedTypes' => ['image'],
      'allowedExtensions' => ['jpg', 'png', 'gif'],
      'validate' => true,
      'mode' => 0777,
      'rename' => false,
      'recursive' => false,
      'overwrite' => false,
    ]);
    

    Does not include the name option which is very helpful, does not explain how name and rename relate, and includes recursive which is not used in the \Leaf\FS\File::upload() code. Instead, mkdir is simply sent true as the recursive argument. ARGH!

                if (!$destinationIsBucket) {
                    $destinationPath = new Path($destination);
                    $destination = $destinationPath->normalize();
    
                    if (!Directory::exists($destination)) {
                        mkdir($destination, $options['mode'], true); // true instead of $options['recursive']! Why? 
                    }
                } else {
                    $file = fopen($temp, 'r+');
                }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions