i want to upload the employee profile picture into mysql database using php. i tried this coding. image uploaded successful but when i fetch that image on browser it shows me text character like this (ÿØÿàJFIFHHÿáLExifMM*bj(1r).

one more problem is below 20kb size images only uploaded into mysql. how to upload large img files into mysql with specific width and height? how to fetch that image?

html page code :

<form enctype="multipart/form-data" action="uploadimg.php" method="post" name="upimg">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">


<?php
    include("dbconfig.php");
    $sql = mysql_query ( "SELECT * FROM reguser" ) or die(mysql_error());
    while($row = mysql_fetch_array($sql))
    {
    echo "<td>" . $row['emp_img'] . "</td>";
    }
    ?>

uploadimg.php code :

<?
include("dbconfig.php");
    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 
  $tmpName  = $_FILES['image']['tmp_name'];  

  $fp = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);
  fclose($fp);


  $query = "INSERT INTO emp_register ";
  $query .= "(emp_img) VALUES ('$data')";
  $results = mysql_query($query, $link) or die(mysql_error());

  print "Success";

  }
  else {
  print "Error";
  }
  ?>

Recommended Answers

All 10 Replies

Why are you uploading images to your database? Wouldn't it be much easier to just upload the images to a specific destination on your server, and then insert the location of that image into your database? E.g. you save your images in /uploads/images and you then save the location of a specific image in your database. For example an image called "image1.jpg" would be saved as "/uploads/images/image1.jpg" and saved as "/uploads/images/image1.jpg" in your database.

because image should be visible when employee open their account? we stored all details in db. so, i thought we can store the image in db. your idea is also good. you are a experienced guy. but i didn't think in your way? how to do that? if you know tell me the two ways to store the img in db. last couple of days you cleared some errors of me. so, i can easily understand when you told about errors...

Well I'm not so fond of storing images in a database, because I don't think that's what a database is for. If you absolutely need to store images in a database I suggest you check out the base64_encode() and base64_decode() functions.

I would however still suggest saving the images in a directory on your server. You can use move_uploaded_file() to save an uploaded image (uploaded through a form) on your server.

I tried storing images in database and trust me, its a real pain to encrypt and decrypt the data.
Just store the image in a folder and use that link to view the images later.

$be_imageName       =   $_FILES["imageName"]["name"];
$be_imageTempName   =   $_FILES["imageName"]["tmp_name"];
$be_imageSize       =   $_FILES["imageName"]["size"];

move_uploaded_file($be_imageTempName,"images/".$be_imageName);

The above code will upload the image to the images folder. Then store the image name in your database. When you want to show the image, just append the image name(taken from database) to the image path and paste it in the <img>.

Note: You can make sure that the images are not overritten by checking if there is a different image with the same file name, by using

$newImageName   =   $be_imageName;
loop1:
if(!file_exists("images/".$be_newImageName)){
    move_uploaded_file($tmpName, $path.$newImageName);
}
else{
    $newImageName .= "_1";
    goto loop1;
}

The above code prevents overwriting of images due to same Image names. You have to store the new Image name in the database.

In addition to that example, you could generate your own unique image names using some random numbers and time variables. Example:

<?php
$extension = ''; // Make sure you get the extension of the upload image.
$random_image_name = md5(mt_rand(0, 100) . time() . mt_rand(0, 100)) . $extension;

generating random image name is a good idea, although there is a chance of a string repetetion. Then you will have a problem. Then again it can be avoided in the same way as I avoided image overwriting.

That is true, but if you use time() as a base for your random image name, the chances that you will get a non-unique name are pretty small :).

I uploaded an image in a folder on server and copied its link in my database.

Now how can i show my image as If i fetch that info it just displays information i.e name of file.jpg

You will need to put that file name inside an <img> tag, I guess ;). And I also think you probably should've posted that question in a new topic, but that's too late now, anyway, I guess.

I know it's already been said, but I really think it should be mentioned again.

Database:
*id,name ,title,age,profile_picture*
{ 1,"Bob","CEO",34,"bobs pic.jpg" }
{ 2,"Bill","Developer",27,"billpic.png" }
PHP
<?php 

    /* Connect to DB and select employee's data */

    echo "<img src='images/employees/".$row['profile_pic']."' />";

?>

Storing images in a database is a bad game you don't want to bother with. It is SOOOOOO much easier to handle and manipulate images when they are stored on the server. Also, images make big fat databases, and databases are usually given less space on a hosting account.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.