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!
Hi again,
On this page: https://leafphp.dev/docs/utils/fs.html we see error reporting for file upload as:
However, in Leaf v. 4.4
request()->upload()does file relocation with\Leaf\FS\File::upload()which logs errors to its ownstatic::errorsArray. This errors array is not merged byrequest()->errors(). The code in \Leaf\Http::errors() shows:This change fixes the issue for me:
I'm having students merge the two errors arrays manually in their code like this:
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!