2

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;
?>
4
  • Something wrong with echo "image: <p /> your image <p /><img src='view.php?id="id"'>";? Commented May 5, 2013 at 8:27
  • @DaveChen but even if i remove it the code does not insert anything Commented May 5, 2013 at 8:29
  • I highlighted a syntax error. Your script shouldn't have been able to run at all. Commented May 5, 2013 at 8:33
  • @DaveChen ok i will do but why the insert does not work too ? Commented May 5, 2013 at 8:37

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Agreed! What about small sprites though?
less than file page size?, Actually it is the same story, Database servers store blobs with page boundaries, so no difference on the size generally. why bother?
Also having multiple images loaded into the PHP memory will more than likely end up causing "Out of Memory" errors. Save files to the file system, only store the path.
0

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!

Comments

0

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

Comments

0

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

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.