jquery - 为 jquery 文件上传脚本创建 jpeg 图像数据

标签 jquery mysql image-upload

我正在使用Jquery File Script替换我现有的上传脚本。在我当前的脚本中,我捕获原始图像数据并将其推送到我的数据库,但它似乎不适用于 jquery 上传脚本。

以下是我在私有(private)函数中使用它的方法:handle_file_upload

private function handle_file_upload($uploaded_file, $name, $size, $type, $error) {
    $file = new stdClass();
    $file->name = $this->trim_file_name($name, $type);
    $file->size = intval($size);
    $file->type = $type;
    $error = $this->has_error($uploaded_file, $file, $error);
    if (!$error && $file->name) {
        $file_path = $this->options['upload_dir'].$file->name;
        $append_file = !$this->options['discard_aborted_uploads'] &&
            is_file($file_path) && $file->size > filesize($file_path);
        clearstatcache();
        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 = filesize($file_path);
        if ($file_size === $file->size) {
                if ($this->options['orient_image']) {
                    $this->orient_image($file_path);
                }
            $file->url = $this->options['upload_url'].rawurlencode($file->name);
            foreach($this->options['image_versions'] as $version => $options) {
                if ($this->create_scaled_image($file->name, $options)) {
                    $file->{$version.'_url'} = $options['upload_url']
                        .rawurlencode($file->name);
                }
            }
        } else if ($this->options['discard_aborted_uploads']) {
            unlink($file_path);
            $file->error = 'abort';
        }
        $file->size = $file_size;
        $file->delete_url = $this->options['script_url']
            .'?file='.rawurlencode($file->name);
        $file->delete_type = 'DELETE';


        // ====== NEW DB CODE ======= //

        $server = 'XXXX';
        $user = 'XXX';
        $pass = 'XXX'; 
        $database= 'XXX';

        $db = mysql_connect($server,$user,$pass);
        $db_select = mysql_select_db($database,$db);

        ob_start(); // Start capturing stdout.
        imagejpeg($uploaded_file); // As though output to browser.
        $image_bin = mysql_real_escape_string(ob_get_contents()); // the raw jpeg image data.
        ob_end_clean(); // Dump the stdout so it does not screw other output.

        $user_id = 5;
        $title = '';
        $caption = '';

        $mapimage = 0;

        $SQL = "INSERT INTO images VALUES (NULL, {$user_id}, '{$title}', "
        . "'{$caption}', '{$image_bin}' )";
        $mrh = mysql_query($SQL);
        if (!$mrh) {
            $res->st=false;
            $res->error="Error creating image record: " .$SQL. mysql_error($dbh);
            return $res;
        }

        // ====== END NEW DB CODE ======= //



    } 
    else {
        $file->error = $error;
    }
    return $file;
}

它插入了图像,但在查看图像时,我看到一个损坏的图像图标,这让我认为它没有插入应有的内容。图像的数据库内容也非常小(字节),这告诉我我没有插入我认为的内容。

知道我可能会错过什么吗?

最佳答案

也许:

imagejpeg($file_path);

而不是:

imagejpeg($uploaded_file);

关于jquery - 为 jquery 文件上传脚本创建 jpeg 图像数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8728667/

相关文章:

php - 将带有缩略图的图像上传到文件夹并添加到 Mysql 的路径

javascript - JQuery .not(find()) 或 - 可以使用吗?

mysql - SQL 语法如何显示相关行?

mysql - 如何: Counting albums and subalbums images and sum images to the main album

php - 个人资料页面 PHP + MySQL

python - Django 在没有 FileField 或 ImageField 和 MEDIAROOT 的特定文件夹中上传图像

node.js - 如何在 Sailsjs --no-frontend API 中通过 URL 访问图像

jquery - 签名未在 html5 中的 Canvas 元素上显示

javascript - jQuery 事件处理程序闭包与上下文

jquery - 在 MVC 中将 Json Datetime 分配给 TextBoxFor 时格式错误