how insert and retrieve images to and from database using php

不羁岁月 提交于 2019-12-07 03:33:27

I guess you should re-think what you are doing.

Why do you store files in a mysql database? I mean what is the point in that?

File system is an expert database for storing files. So better do not keep your files in a normal database but in the file system.

Images can be named with numbers in directories (0102003.gif) and can be addressed via the filenames in your mysql records.

If you really need file storage in a database, mysql is not the tool for that.

Have a look at mongoDB.

PS: mysql interface is deprecated, please use mysqli or PDO.

Shenal

Try This code it will help you.

<?php
    mysql_connect("localhost", "root","") or die ("could not connect to the server");
    mysql_select_db("database1") or die ("that database could not be found");

    $file = $_FILES['image']['tmp_name'];

    $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    mysql_query("INSERT INTO table1 (id,image) VALUES ('1','{$image}')");        
?>

To store images on a php website, you just have to put the files in a directory and save the filenames on the mySQL database. Goodluck!

You need enctype=multipart/form-data in your form declaration. And access the file through the $_FILES variable instead of the $_POST variable. Like:

<form action="testimage1.php" method="post" enctype="multipart/form-data"> <input name="uploadimage" type="file" /> </form> <?php $filename = $_FILES['uploadimage']['tmp_name']; ?>

There are 2 different ways by which you can insert and retrieve images:

Method 1:

Store entire image in the database itself in longblob type and retrieve it from the database.

Insert image:

<?php
//Get uploaded file data
$img_tmp_name    = $_FILES['image']['tmp_name'];
$img_name        = $_FILES['image']['name'];
$img_size        = $_FILES['image']['size'];
$img_type        = $_FILES['image']['type'];
$img_error       = $_FILES['image']['error'];
//Processed uploaded img data
if($img_error > 0)
{
    die('Upload error or No img uploaded');
}
$handle = fopen($img_tmp_name, "r");
$content = fread($handle, $img_size);
fclose($handle);
$encoded_content = addslashes($content);

$query="insert into user_info (img_content,img_type) values('".$encoded_content."','".$img_type."')";
$ex=mysqli_query($conn,$query);
if($ex){
    echo "image uploaded";
}
else{
    echo "image not uploaded<br/>".mysqli_error($conn);
}
?>

Retrieve image:

You can retrieve an image by 2 methods, first by using a handler PHP script to return the image data and the second one as follows:

<?php
    $query="select * from user_info WHERE id='".$id."'";
    $ex=mysqli_query($conn,$query);
    $row=mysqli_fetch_array($ex)
?>              
<img src="<?php echo "data:'".$row['img_type']."';base64,".base64_encode($row['img_content']); ?>" />

But method 1 is not a recommended way if the image size will be large.

Method 2:

In this method, we only store the path of the image in the database. We store the images in a directory.

Insert image:

<?php
    $target_dir = "image/";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);

    $query="insert into user_info (img_path) values('".$target_file."')";
    $ex=mysqli_query($conn,$query);
    if($ex){
        echo "image uploaded";
    }
    else{
        echo "image not uploaded<br/>".mysqli_error($conn);
    }
?>

Retrieve image:

<?php
    $query="select * from user_info WHERE id='".$id."'";
    $ex=mysqli_query($conn,$query);
    $row=mysqli_fetch_array($ex)
?>
<img src="<?php echo $row['img_path']; ?>" alt=""/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!