how insert and retrieve images to and from database using php

末鹿安然 提交于 2019-12-08 05:13:29

问题


im trying to upload an image for a member profile and store it in a database using php then retrieve it but its does not work with me

this is what im trying for inserting the image :

<html>
<head>
<title> Upload an image </title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/from/data">
  File:
  <input type="file" name="image" > <input type="submit" value="upload">
</form>
</body>
</html>
<?php

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

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

  if (!isset($file))
     echo "please select file";
  else
  {

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

     if($image_size == FALSE)
        echo "that not image ";

     else
     {

         if (!$insert= mysql_query("INSERT INTO user_info (image) VALUES ('$image')"))
             echo "Problem";

         else
         {

             echo "image: <p /> your image <p /><img src='view.php?id="id"'>";
         }
     }


 }

and this for retrieving the image

<?php

   // do some validation here to ensure id is safe
   mysql_connect("localhost", "root","") or die ("could not connect to the server");
   mysql_select_db("project") or die ("that database could not be found");
   $id = addslashes($_REQUEST['id']);

   $image = mysql_query("SELECT * FROM user_info WHERE id='$id'");
   $image = mysql_fetch_assoc($image);
   $image = $image['image'];

   header("Content-type: image/jpeg");
   echo $image;
?>

回答1:


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.




回答2:


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}')");        
?>



回答3:


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!




回答4:


i hope this like use full to you:-

http://forgetcode.com/PHP/539-Photo-Upload-and-Retrive-with-MySql-using-BLOB




回答5:


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']; ?>




回答6:


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=""/>


来源:https://stackoverflow.com/questions/16382672/how-insert-and-retrieve-images-to-and-from-database-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!