php - Unable to retrieve image data from table and display image -
i'm trying upload image file database php , mysqli, i'm coming across confusing piece of error. table has 2 column, 'title' , 'image'. 'title' file's name , 'image' image data. both tables not allowed accept nulls. when upload file, data stored table columns. 'title' contains right values, 'image' column contains '<binary data>
'.
since table column not accept null values assumed file's data, when try retrieve , display image data in showimage.php tells me image data null.
i using blob datatype store image data in table. far concerned based off online resources , examples should work. thanks.
code:
php:
uploads.php
if (isset($_post['submit'])) { $title = $_files['image']['name']; $data = $_files['image']['tmp_name']; $content = file_get_contents($data); $query = "insert images (title, image) values (?, ?)"; $statement = $databaseconnection->prepare($query); $statement->bind_param('sb', $title, $content); $statement->execute(); $statement->store_result(); $creationwassuccessful = $statement->affected_rows == 1 ? true : false; if ($creationwassuccessful) { echo "works!"; } else { echo 'failed'; } }
showimage.php
if (isset($_get['id'])) { $id = $_get['id']; $query = "select * images id = ?"; $statement = $databaseconnection->prepare($query); $statement->bind_param('i', $id); $statement->execute(); $statement->store_result(); if ($statement->num_rows >= 1) { $statement->bind_result($imageid, $title, $image) while ($statement->fetch()) { if ($image == null) { echo "image data not exist!"; } else { header("content-type: image/jpeg"); echo $image; } } } }
html
<form action="uploads.php" method="post" enctype="multipart/form-data"> <input type="file" name="image"> <input type="submit" name="submit"> </form>
first need save image output file_get_contents
database. , put imagecreatefromstring
, show image.
here's simple example. maybe out :)
$data = file_get_contents("acl.jpg"); $img = imagecreatefromstring($data); header("content-type: image/jpeg"); imagejpeg($img);
edit :
you need put code :
$statement->bind_result($imageid, $title, $image) while ($statement->fetch()) { if ($image == null) { echo "image data not exist!"; } else { $img = imagecreatefromstring($image); header("content-type: image/jpeg"); imagejpeg($img); } }
edit fix :
uploads.php
in file need change $statement->bind_param('sb', $title, $content);
become $statement->bind_param('ss', $title, $content);
if (isset($_post['submit'])) { $title = $_files['image']['name']; $data = $_files['image']['tmp_name']; $content = file_get_contents($data); $query = "insert images (title, image) values (?, ?)"; $statement = $databaseconnection->prepare($query); $statement->bind_param('ss', $title, $content); $statement->execute(); $statement->store_result(); $creationwassuccessful = $statement->affected_rows == 1 ? true : false; if ($creationwassuccessful) { echo "works!"; } else { echo 'failed'; } }
showimage.php
, in show using :
$img = imagecreatefromstring($image); header("content-type: image/jpeg"); imagejpeg($img);
in last statement
if (isset($_get['id'])) { $id = $_get['id']; $query = "select id,title,image images id = ?"; $statement = $databaseconnection->prepare($query); $statement->bind_param('i', $id); $statement->execute(); $statement->store_result(); if ($statement->num_rows >= 1) { $statement->bind_result($imageid, $title, $image) while ($statement->fetch()) { if ($image == null) { echo "image data not exist!"; } else { $img = imagecreatefromstring($image); header("content-type: image/jpeg"); imagejpeg($img); } } } }
hope work fine too, i've tested , run well... :)
Comments
Post a Comment