File Upload Validation Broke |
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"> Controller: Code: <?php namespace App\Controllers; Validation.php: Code: public $file_upload_valid = [ 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).
Has anyone had validation working on file uploads? (as per the explanation given in the User Guide: https://codeigniter.com/user_guide/libra....html#id27) ?... I'm still having no joy in my attempts to get any of the listed rules working?
Thanks. (04-23-2020, 02:06 PM)Gary Wrote: Has anyone had validation working on file uploads? (as per the explanation given in the User Guide: https://codeigniter.com/user_guide/libra....html#id27) ?... I'm still having no joy in my attempts to get any of the listed rules working? Hi, I decided to try a quick test (added validation to my existing image uploading stuff): PHP Code: $validation = \Config\Services::validation(); and this is view: PHP Code: echo \Config\Services::validation()->listErrors() ?? null; No, not working. I get an error in my view file saying the image is too big. But it is definitely not too big. Not having any problem with the getFile() function or getFiles(), without the validation stuff I just added, of course. p.s. I'm not using ajax
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Thanks for that Leo.
It consistently fails on whatever the first test is... regardless of how one sets up the validation (I've even tried the calling with the parameters directly (without setting the rules) as in the example: $this->validate(['avatar'=>'uploaded[avatar]|max_size[avatar,1024]']) with no success). I see you're doing multiple files... which I've also resorted to using (with getFiles()), even for a single file (which I then "separate" out of the upload using foreach($files_upload as $file)...) which works. I've still not been able to get getFile() working for some reason.
I upload single files also, try this simplified method in some controller:
PHP Code: public function testing()
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Great, thanks for that Leo… your code worked, straight out the box.
Although it wasn’t too different to many of my previous hacks at getting getFile() working, it helped clarify things for me pretty quickly… primarily because the one notable departure from your working example code is that my file/s are uploaded using a JavaScript post (which, because they may be really large, permits an easy-to-implement % progress indication on the client-side). So, although your code has identified where the getFile() problem is, thank you... I still haven’t found a way to get the JavaScript post working for a single file (other than stepping through the multiple file uploads array, as mentioned earlier). When using getFile(), any existence and/or isValid() checks always fail because the expected POST/REQUEST/FILE that should contain an instance of the uploaded file never appears to get populated using getFile() if the post comes via a JavaScript post?!? I have snooped around in the framework code a bit (particularly \system\HTTP\Files\FileCollection.php, and what it references), and see that although getFile() and getFiles() functions look similar, under the bonnet they are different- particularly in how they get the objects they return… possibly why getFiles() always returns something to work with, whereas getFile() always returns NULL for my JavaScript posts. Without knowing enough, all I can guess is that there is something not quite right with getFile(), or perhaps getFiles()… although getFiles() works (and perhaps it shouldn’t ?). I suspect that, to have a similar response, getFiles() should probably be using FileCollection->getFileMultiple() vs the FileCollection->all() it currently uses (?). The only references to getFileMuliple() occur in IncomingRequest.php and FileCollection.php, and… as fate would have it… also in (the apparently non-functional) file Validation (in FileRules.php), but, other than that, doesn’t appear to be used by the framework.
You know what, I got validation to work...still without JavaScript though - I don't have any progress showing things made, to test with.
PHP Code: public function testing_uploads_validation()
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Great, thanks Leo.
I'll give the way you've done it a try. I had thought I had it working at some point a few days ago (not done the way you have it now, though)... but it wasn't the main focus of attention at the time, and when I came back to work on this portion of the code again, it was broke... and I can't be sure whether it was the code that was working, or the red wine. As an aside, I see you have a helper('form') fetish... and there's probably no harm in that, though, if you have a look in the user manual (under Form Helper), it says this: If you use any of the form helper functions listed on this page, and you pass values as an associative array, the form values will be automatically escaped, so there is no need to call this function. Use it only if you are creating your own form elements, which you would pass as strings.
Also, this may or may not help you. I have this set up currently for a wysiwyg plugin. I didn't have the time to fiddle with it, it just uses native PHP and works with csrf on. I just tested it out, to make sure I won't have to go through what you are goin through now
![]() Javascript Code: $('.wysiwyg').summernote({ Controller: PHP Code: public function wysiwyg_upload() P.S. the form wouldn't generate without the helper. I mean the functions below helper('form'); in my example wouldn't work. P.P.S. I'll try to convert native PHP to use codeigniter func's to test for bugs, later on, to see if I get getFile() errors like you.
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
|
Welcome Guest, Not a member yet? Register Sign In |