how to store the images in the mysql table and retrieval of stored image using php code?
i need help that how to store the images in the mysql table and retrieval of stored image using 开发者_Python百科php code...
sample code that helps me a lot..
regards ~Deepu~
Create a table in database with Blob field and another varchar field for picture type (jpeg/gif/etc..), store the picture in there.
To store the picture do the following:
- Read the picture into variable. Either
fread
offile_get_contents
- Insert picture data and picture type into database
To retrieve picture back do regular select statement to get the picture data and file type.
Set the header Content-type
to appropriate file type and display the picture data.
For example:
HTML
<img src="getPicture.php?id=12345" />
PHP
<?php
$id = (int) $_GET['id'];
// Assume $db is out DAL that is already connected and can query database
$img = $db->loadObject("SELECT pic_data, pic_type FROM picture WHERE id = $id LIMIT 1");
// We get the following
// $img->pic_type = 'image/jpeg'
// $img->pic_data = 'picture data'
//
//
// Make sure there is not output prior setting header
header("Content-type: $img->pic_type");
echo $img->pic_data;
Look at this page. There is code it will help you http://www.anyexample.com/programming/php/php_mysql_example__image_gallery_(blob_storage).xml
To store the image directly in the table...
Onchange event can be used to preview the selected image.
After form submit the image is temporarly stored in $_FILES['user_photo']['tmp_name'], file_get_contents() and base64_encode() are two internal functions to get the content of the image and convert it to base64,
Then the image is stored in the mySQL table by insert query and the entire table is fetched by running the select query and displayed in the table.
Create a table in the database with three fields id, name, and image. Set PRIMARY KEY for id, set VARCHAR(30) for name and LONGBLOB for image.
SQL Query for creating a table
CREATE TABLE `image_database` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(30) NOT NULL ,
`image` LONGBLOB NOT NULL , PRIMARY KEY (`id`));
preview.html
<body>
<form method="post" action="actionPage.php" enctype="multipart/form-data" >
<table>
<tr>
<td>Enter your name :</td>
<td><input type="text" name="firstName" placeholder="Enter Your Name"/></td>
</tr>
<tr>
<td>Choose an Image</td>
<td><input type="file" name="user_photo" onchange="preview()"/></td>
<td><img src="" width="100px" height="150px" id="frame"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" /></td>
</tr>
</table>
</form>
</body>
<script type="text/javascript">
function preview(){
frame.src=URL.createObjectURL(event.target.files[0]);
}
</script>
Give Your SQL(PHP myAdmin) User Name, Password & Database Name.
actionPage.php
<?php
$db_host = "localhost";
$db_user_name = "root"; //Enter your database user name
$db_password = "*****"; //Enter the password
$db_name ="__________"; //Enter your database name
$db_connection = mysqli_connect($db_host, $db_user_name, $db_password, $db_name);
$firstName=$_POST["firstName"];
$image=$_FILES['user_photo']['tmp_name'];
$image=file_get_contents($image);
$image=base64_encode($image);
$insert_query="INSERT INTO image_database(name, image) VALUES ('$firstName', '$image')";
$db_connection->query($insert_query);
$select_query="SELECT * FROM image_database";
$result = $db_connection->query($select_query);
?>
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Image</td>
</tr>
<?php while($row=$result->fetch_row()){?>
<tr>
<td><?php echo $row[0]; ?></td>
<td><?php echo $row[1]; ?></td>
<td><?php echo '<img src="data:image;base64,'.$row[2].'"; width="100px"; >';?></td>
</tr>
<?php } ?>
</table>
精彩评论