Welcome Guest, Not a member yet? Register   Sign In
File Upload Validation Broke
#10

(This post was last modified: 04-25-2020, 02:18 PM by Leo.)

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 Smile its simpler then a "progress bar" type thing, it works for multiple files upload.


Javascript
Code:
$('.wysiwyg').summernote({
            //////irrelevant wysiwyg summer note stuff////no time to polish the function below to be stand alone/////
            callbacks: {
                onImageUpload: function (files) {
                    for (var i = 0; i < files.length; i++) {
                        send_wysiwyg(files[i]);
                    }
                }
            }
        });

function send_wysiwyg(file) {
        if (file.type.includes('image')) {
            var name = file.name.split(".");
            name = name[0];
            var data = new FormData();
            data.append('file', file);
            data.append("csrf_token", "<?=csrf_hash()?>");
            $.ajax({
                url: '/media/wysiwyg_upload',
                type: 'POST',
                contentType: false,
                cache: false,
                processData: false,
                dataType: 'JSON',
                data: data,
                headers: {'X-Requested-With': 'XMLHttpRequest'},
                success: function (response) {
                    if (response['status'] === 'success') {
                        $('.wysiwyg').summernote('insertImage', response['url'], name);
                        update_csrf_fields(response.csrf_token);
                    } else {
                        console.log(response['status']);
                    }
                }
            })
                .fail(function (e) {
                    console.log(e);
                });
        } else {
            console.log("That's not an image");
        }
    }

Controller:
PHP Code:
public function wysiwyg_upload()
    {
        if ($this->request->isAJAX()) {
            $confirm_csrf $this->request->getRawInput();
            if (isset($_FILES)) {
                if (!$_FILES['file']['error']) {
                    if (preg_match("/image/"$_FILES['file']['type'])) {
                        $name time() . rand(100999);
                        $ext explode('.'$_FILES['file']['name']);
                        $filename $name '.' $ext[1];
                        $destination UPLOAD_FILES $filename;
                        $location $_FILES["file"]["tmp_name"];
                        move_uploaded_file($location$destination);
                        $response['url'] = STATIC_FILES 'uploads/' $filename;
                        $response['status'] = 'success';
                        
$response['csrf_token'] = csrf_hash();
                    } else {
                        $response['status'] = 'Its not an image';
                    }
                } else {
                    $response['status'] = "The image couldn't load. Error(" $_FILES['file']['error'] . ")";
                }
            } else {
                $response['status'] = "There is no spoon.....file.";
            }

            return $this->response->setJSON($response);
        }
        return '{"error":"Invalid Request"}';
    

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.
Reply


Messages In This Thread
File Upload Validation Broke - by Gary - 04-12-2020, 11:51 AM
RE: File Upload Validation Broke - by Gary - 04-14-2020, 07:00 PM
RE: File Upload Validation Broke - by Gary - 04-23-2020, 02:06 PM
RE: File Upload Validation Broke - by Leo - 04-23-2020, 02:49 PM
RE: File Upload Validation Broke - by Gary - 04-23-2020, 03:18 PM
RE: File Upload Validation Broke - by Leo - 04-23-2020, 03:55 PM
RE: File Upload Validation Broke - by Gary - 04-24-2020, 08:47 AM
RE: File Upload Validation Broke - by Leo - 04-24-2020, 02:31 PM
RE: File Upload Validation Broke - by Gary - 04-24-2020, 03:03 PM
RE: File Upload Validation Broke - by Leo - 04-24-2020, 03:09 PM
RE: File Upload Validation Broke - by Gary - 04-24-2020, 03:40 PM
RE: File Upload Validation Broke - by Leo - 04-24-2020, 03:46 PM
RE: File Upload Validation Broke - by Gary - 04-24-2020, 03:58 PM
RE: File Upload Validation Broke - by Leo - 04-25-2020, 01:13 AM
RE: File Upload Validation Broke - by Leo - 04-25-2020, 02:56 AM
RE: File Upload Validation Broke - by Gary - 04-28-2020, 09:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB