Php Wordpress-从存储为blob数据的数据库中获取图像
我有一个表单上传到数据库,并将图像存储为blob。我无法让它们输出 上传代码:Php Wordpress-从存储为blob数据的数据库中获取图像,php,mysql,wordpress,blob,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) {
$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中使用以下命令
要使其正常工作,我需要做哪些更改?您正在获取表中该产品id的所有信息,然后尝试将其显示为图像。您需要从结果数组访问图像或仅选择图像,例如,使用get_var而不是get_results,选择img而不是*:
顺便说一句,这会让您接受SQL注入,因此您确实应该使用$wpdb->prepare在查询中包含一个变量,例如
$image = $wpdb->get_var(
$wpdb->prepare("SELECT img FROM products WHERE id = %d", $product_id)
);
水滴大小限制
请注意,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.'" />';
上面的代码未经测试,但基本思想是存在的。同样,如果一个同名的文件已经存在,它将不起作用,所以您可能需要考虑为该名称添加一个时间戳以使其具有唯一性。
参考:
什么不起作用?这没有告诉我们多少。您尝试调试了什么-图像是否没有返回,图像是否返回但没有正确显示,或者您是否从数据库中得到了任何结果?当我运行$wpdb->prepare选项时,我没有得到任何返回,但是如果我检查代码,我会从您的注释中得到第一个选项。如果我运行print\r$img,我会看到所有的图像数据。好的,我们将首先集中精力使其工作。你试过使用echo吗;要显示它?是$image=$wpdb->get\u varSELECT img FROM products,其中id=$product\u id;回响返回尝试headerContent类型时发生的情况:image/jpeg;echo$image;?另外,数据库中的列类型是什么,您上载的图像大小是多少?
$image = $wpdb->get_var(
$wpdb->prepare("SELECT img FROM products WHERE id = %d", $product_id)
);
/* 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.'" />';