adding the uploaded file name to database in blueimp fileupload jquery plugin

◇◆丶佛笑我妖孽 提交于 2019-12-02 05:28:12

问题


I am hoping someone has some experience with the blueimp fileupload jquery plugin at : https://github.com/blueimp/jQuery-File-Upload

How to add the uploaded file name to database ?


回答1:


In the options array (look for $this->options = array( ) and insert

'database' => 'database_name',  
'host' => 'localhost',  
'username' => 'user',  
'password' => 'password',  

Then after

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
        $index = null, $content_range = null) {
    $file = new stdClass();
    $file->name = $this->get_file_name($name, $type, $index, $content_range);
    $file->size = $this->fix_integer_overflow(intval($size));
    $file->type = $type;</code></pre>

Insert this code

    //Start added coded
    // prepare the image for insertion
    $data = addslashes (file_get_contents($uploaded_file));

    // get the image info..
    $size = getimagesize($uploaded_file);     

    $file->upload_to_db = $this->add_img($data, $size, $name);

    //end added code

and after the function handle_file_upload insert the following code to actually upload the image to the database.

function query($query) {  
    $database = $this->options['database'];  
    $host = $this->options['host'];  
    $username = $this->options['username'];  
    $password = $this->options['password'];  
    $link = mysql_connect($host,$username,$password);  
    if (!$link) {  
        die(mysql_error());  
    }
    $db_selected = mysql_select_db($database);  
    if (!$db_selected) {  
        die(mysql_error());  
    }  
    $result = mysql_query($query);  
    mysql_close($link);  
    return $result;  
}  

function add_img($data,$size,$name)  
{  
    $add_to_db = $this->query("INSERT INTO your_database_name
            (image_type ,image, image_size, file_name)
            VALUES
            ('{$size['mime']}', '{$data}', '{$size[3]}', '{$name}')") or die(mysql_error());  
    return $add_to_db;  
}

This will store the actual image in the database if you don't want that change the add_img($data,$size,$name) to add_img($size,$name) and just don't pass the $data variable. The $data variable should be stored as a medium or long blob.

You can also comment out the fileupload to directory stuff so you don't get errors if you don't what the images uploaded to a directory. This is in the protected function handle_file_upload

//comment out file upload stuff since storing in database
    /*
    if ($this->validate($uploaded_file, $file, $error, $index)) {
        $this->handle_form_data($file, $index);
        $upload_dir = $this->get_upload_path();
        if (!is_dir($upload_dir)) {
            mkdir($upload_dir, $this->options['mkdir_mode'], true);
        }
        $file_path = $this->get_upload_path($file->name);
        $append_file = $content_range && is_file($file_path) &&
            $file->size > $this->get_file_size($file_path);
        if ($uploaded_file && is_uploaded_file($uploaded_file)) {
            // multipart/formdata uploads (POST method uploads)
            if ($append_file) {
                file_put_contents(
                    $file_path,
                    fopen($uploaded_file, 'r'),
                    FILE_APPEND
                );
            } else {
                move_uploaded_file($uploaded_file, $file_path);
            }
        } else {
            // Non-multipart uploads (PUT method support)
            file_put_contents(
                $file_path,
                fopen('php://input', 'r'),
                $append_file ? FILE_APPEND : 0
            );
        }
        $file_size = $this->get_file_size($file_path, $append_file);
        if ($file_size === $file->size) {
            $file->url = $this->get_download_url($file->name);
            list($img_width, $img_height) = @getimagesize($file_path);
            if (is_int($img_width) &&
                    preg_match($this->options['inline_file_types'], $file->name)) {
                $this->handle_image_file($file_path, $file);
            }
        } else {
            $file->size = $file_size;

            if (!$content_range && $this->options['discard_aborted_uploads']) {
                unlink($file_path);
                $file->error = 'abort';
            }
        }
        $this->set_additional_file_properties($file);
    }
    */


来源:https://stackoverflow.com/questions/17185290/adding-the-uploaded-file-name-to-database-in-blueimp-fileupload-jquery-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!