Wordpress - 将数据库中的图像存储为blob数据

时间:2017-09-19 16:30:52

标签: php mysql wordpress blob

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

上传代码:

$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但是这个文件也没有用,例如

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);

我需要更改什么才能让它发挥作用?

1 个答案:

答案 0 :(得分:2)

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

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

顺便说一下,这让你开始注入SQL,所以你真的应该使用$wpdb->prepare在你的查询中包含一个变量,例如。

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

BLOB大小限制

请注意,MYSQL中的BLOB限制为64kb。如果您的图像大于此值,则需要使用16MB

的MEDIUMBLOB

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

更实际的解决方案是只保存路径。

要执行此操作,您需要创建一个文件夹以上传图像(例如,在我的代码中,在Web根目录中,并在其中调用myimages),以及数据库中的新VARCHAR或TEXT列(在下面的示例中称为img_path)。

/* 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.";
}

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

$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.'" />';

上面的代码未经测试,但基本的想法就在那里。此外,如果已存在同名文件,它将无法工作,因此您可能需要考虑在名称中添加时间戳以使其唯一。

参考