Although the infinite monkey theorem will surely solve this at some point… maybe… the monkeys are becoming restless.
Whilst attempting to have a file upload validated, no results other than the file->isValid() standard failure responses (that particular code is not shown here) or a consistent Validation error message (always fails the first check, regardless of what the check is) are obtainable…
It appears as though:
1) getFile() always returns with a NULL
2) getFiles() although returning an array of files (which contains only one in this case), the Validation routine can’t find the details in this array and fails
I’ve tried a variety of things to get the single file’s FileCollection() instance so that something like $validationResult = $validation->run($file, 'file_upload_valid'); could be tried, but nothing seems to work?
Any ideas on what’s broken?
Thanks.
HTML:
Code:
<form id="form_upload" class="form-horizontal" action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">
...
echo form_upload(['id' => 'files_upload', 'name' => 'file_name', 'class' => 'form-control', 'value' => '' ]); // 'type' => 'file' done automatically
Controller:
Code:
<?php namespace App\Controllers;
// use CodeIgniter\HTTP\RequestInterface;
use App\Libraries\Functions;
// use Config\Vars;
class Upload extends BaseController {
...
public function upload () {
$file = $this->request->getFile('file_name'); // get the single file
// echo $file->getName(); // RESULT: Error - Call to a member function getName() on null if getFile() is used. Obviously also fails with “Error - Call to a member function getName() on array” if getFiles() is used.
print_r($file); // RESULT: NULL if getFile() is used. If getFiles() is used: Array ( [8oauxy0g0zd31_jpg] => CodeIgniter\HTTP\Files\UploadedFile Object ( [path:protected] => C:\xa…
$validationResult = $this->validate('file_upload_valid');
echo($validation->getError('file_name')); // RESULT: “Please select an upload file”, or if the first rule is removed, then the second rule fail error message…
Validation.php:
Code:
public $file_upload_valid = [
'file_name' => [
'label' => 'upload',
'rules' => 'uploaded[file_name]|ext_in[file_name,jpg,jpeg,gif,png]|is_image[file_name]',
'errors' => [
'uploaded' => 'Please select an {field} file',
...
The upload is done via an AJAX script, that definitely uploads the file - the request payload looks as one would expect:
-----------------------------202171451529518883343801695575
Content-Disposition: form-data; name="tp_csrft"
7d039a403950a52c36d9e2b5562ec107
-----------------------------202171451529518883343801695575
Content-Disposition: form-data; name="8oauxy0g0zd31.jpg"; filename="8oauxy0g0zd31.jpg"
Content-Type: image/jpeg
ÿØÿà. . .
Some additional code (not shown above) moves the new upload to the writable/uploads directory (which does happen on every form submission).