问题
I have a form uploading to database and storing images as blob. I cannot get them to output.
Upload code:
$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,
)
);
Code to display the 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).'" />';
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 . '" />';
with the following in 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?
回答1:
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);
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 size limitation
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
Save the image path instead of directly in the database as a BLOB
A more practical solution is to save just the 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.
Ref:
- PHP POST method uploads
- move_uploaded_file
来源:https://stackoverflow.com/questions/46305530/wordpress-getting-images-from-db-stored-as-blob-data