Wordpress-从数据库获取图像作为Blob数据存储 [英] Wordpress - getting images from db stored as blob data

查看:58
本文介绍了Wordpress-从数据库获取图像作为Blob数据存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单上传到数据库并将图像存储为blob.我无法输出它们.

I have a form uploading to database and storing images as blob. I cannot get them to output.

上传代码:

$image =  strip_tags(file_get_contents($_FILES['image']['tmp_name']));
$image_name = strip_tags($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size == FALSE) {
    echo 'An image has not been uploaded';
} else {
$wpdb->insert( 
    $table, 
    array( 
        'title' => $title,
        'description' => $description,
        'brand' => $brand,
        'img_name' => $image_name,
        'img' => $image,
    )
); 

显示图像的代码:

$product_id = get_query_var('id');

$image = $wpdb->get_results( "SELECT * FROM  products WHERE id = $product_id");

header("Content-Type: image/jpeg");

echo '<img src="data:image/jpeg;base64,'.base64_decode($image[0]->img).'" />';

我也尝试过调用一个单独的文件 get.php ,但这既不起作用,例如

I've also tried calling a separate file get.php but that didnt work either e.g.

echo '<img src="/wp-content/themes/theme/scripta/get.php?id=' . $product_id . '" />';

get.php

$id = $_GET['id'];

$image = $wpdb->get_results( "SELECT * FROM  products WHERE id = $id");

header("Content-Type: image/jpeg");

print_r($image);

要使它正常工作,我需要更改什么?

What do I need to change to get this to work?

推荐答案

您正在表中获取该产品ID的所有信息,然后尝试将其显示为图像.您需要从结果数组或 SELECT 中访问图像,例如图像.使用 get_var 代替 get_results ,然后选择 img 代替 * :

You are getting all information in the table for that product id, and then trying to display it as an image. You need to access the image from the results array or SELECT just the image e.g. use get_var instead of get_results and select img instead of *:

$product_id = get_query_var('id');
$image = $wpdb->get_var("SELECT img FROM products  WHERE id = ".$product_id);

顺便说一句,这让您可以进行SQL注入,因此您确实应该使用 $ wpdb-> prepare 在查询中包含变量,例如

By the way, this leave you open to SQL injection, so you really should use $wpdb->prepare to include a variable in your query, e.g.

$image = $wpdb->get_var( 
    $wpdb->prepare("SELECT img FROM products  WHERE id = %d", $product_id)  
);

BLOB大小限制

请注意,MYSQL中的BLOB限制为64kb.如果您的图片大于此尺寸,则需要使用16MB的MEDIUMBLOB

Note that a BLOB in MYSQL is limited to 64kb. If your images are larger than this, you will need to use a MEDIUMBLOB which is 16MB

将图像路径保存为BLOB,而不是直接在数据库中保存

更实用的解决方案是仅保存路径.

A more practical solution is to save just the path.

为此,您将需要创建一个文件夹以将图像上传到(例如,在下面的我的代码中,在网络根目录中并称为 myimages ),以及一个新的VARCHAR或TEXT列在您的数据库中(在下面的示例中为 img_path ).

To do this, you will need to create a folder to upload the images to (e.g. in my code below, its in the web root and called myimages), and a new VARCHAR or TEXT column in your database (its called img_path in the example below).

/* 1. Define the path to the folder where you will upload the images to, 
      Note, this is relative to your web root. */ 
define (MY_UPLOAD_DIR, "myimages/");

$imagefilename = basename( $_FILES['image']['name']);

/* 2. define the full path to upload the file to, including the file name */
$fulluploadpath = get_home_path(). MY_UPLOAD_DIR . $imagefilename;

$image_name = strip_tags($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);

/* 3. Do your validation checks here, e.g. */
if($image_size == FALSE) {
    echo 'The image was not uploaded';
} 
/* 4. if everything is ok, copy the temp file to your upload folder */
else if(move_uploaded_file($_FILES['image']['tmp_name'], $fulluploadpath )) {
    /* 5. if the file was moved successfully, update the database */
    $wpdb->insert( 
        $table, 
        array( 
            'title' => $title,
            'description' => $description,
            'brand' => $brand,
            'img_name' => $image_name,
            'img_path' => MY_UPLOAD_DIR . $imagefilename, /* save the path instead of the image */
        )
    ); 

} else {
    //Display an error if the upload failed
    echo "Sorry, there was a problem uploading your file.";
}

要从数据库中检索图像并显示它,

To retrieve the image from the database and display it:

$product_id = get_query_var('id');
/* query the database for the image path */
$imagepath = $wpdb->get_var( 
    $wpdb->prepare("SELECT img_path FROM products  WHERE id = %d", $product_id)  
);

/* 6. Display the image using the path. 
      Because the path we used is relative to the web root, we can use it directly 
      by prefixing it with `/` so it starts at the webroot */ 
if ($imagepath)
    echo '<img src="/'.$imagepath.'" />';

上面的代码未经测试,但是基本思想就在那里.另外,如果已经存在具有相同名称的文件,则该功能将不起作用,因此您可能需要考虑在名称中添加时间戳以使其唯一.

The code above is untested, but the basic idea is there. Also, it won't work if a file with the same name already exists, so you might want to consider adding a timestamp to the name to make it unique.

参考:

这篇关于Wordpress-从数据库获取图像作为Blob数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆