How do I upload an image to a table in b. d. MySQL?

There is such a code, uploading an image to images/upload And it is necessary in the database, in the users table in the avatar cell

Images.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Images extends CI_Controller {

    private $upload_path = "./uploads";

    public function index()
    {
        $this->load->view("index");
    }

    public function upload()
    {
        if ( ! empty($_FILES)) 
        {
            $config["upload_path"]   = $this->upload_path;
            $config["allowed_types"] = "gif|jpg|png";
            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload("file")) {
                echo "failed to upload file(s)";
            }
        }
    }

    public function remove()
    {
        $file = $this->input->post("file");
        if ($file && file_exists($this->upload_path . "/" . $file)) {
            unlink($this->upload_path . "/" . $file);
        }
    }

    public function list_files()
    {
        $this->load->helper("file");
        $files = get_filenames($this->upload_path);
        // we need name and size for dropzone mockfile
        foreach ($files as &$file) {
            $file = array(
                'name' => $file,
                'size' => filesize($this->upload_path . "/" . $file)
            );
        }

        header("Content-type: text/json");
        header("Content-type: application/json");
        echo json_encode($files);
    }

}

<script>
    Dropzone.autoDiscover = false;
    var myDropzone = new Dropzone("#my-dropzone", {
        url: "<?php echo site_url("images/upload") ?>",
        acceptedFiles: "image/*",
        addRemoveLinks: true,
        removedfile: function(file) {
            var name = file.name;

            $.ajax({
                type: "post",
                url: "<?php echo site_url("images/remove") ?>",
                data: { file: name },
                dataType: 'html'
            });

            // remove the thumbnail
            var previewElement;
            return (previewElement = file.previewElement) != null ? (previewElement.parentNode.removeChild(file.previewElement)) : (void 0);
        },
        init: function() {
            var me = this;
            $.get("<?php echo site_url("images/list_files") ?>", function(data) {
                // if any files already in server show all here
                if (data.length > 0) {
                    $.each(data, function(key, value) {
                        var mockFile = value;
                        me.emit("addedfile", mockFile);
                        me.emit("thumbnail", mockFile, "<?php echo base_url(); ?>uploads/" + value.name);
                        me.emit("complete", mockFile);
                    });
                }
            });
        }
    });
</script>

Archive with the site

Author: Suvitruf - Andrei Apanasik, 2020-02-12

1 answers

You can use the BLOB field and fill it in. stack with a similar question. But this is the wrong way.

It is better if the uploaded files are stored in .. / public/uploads/, and the database stores the path to them. If there are a lot of downloads, it is better to store them hierarchically uploads/year/month

In the future, it is possible to use a hosting service that will store images not on the server itself, but in the cloud, as AWS S3 does, for example.

 1
Author: Tropen, 2020-02-12 20:18:53