Updating Images (BLOB) in MySQL with PHP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Php Beginner
Forum Commoner
Posts: 28
Joined: Fri Nov 04, 2011 9:04 am

Updating Images (BLOB) in MySQL with PHP

Post by Php Beginner »

Hi there,

I am on my way to create the feature for users to upload their profile photo to the database. I manage to upload my photo to database in binary form (BLOB) but I am having problem while trying to display it.

upload form code:

Code: Select all

<?php //get the posted image when the submit button is clicked 
$username = "MentorMenteeData"; 
$password = "mentormenteedata"; 
$host = "localhost"; 
$database = "mentormenteesystem"; 

// Make the connect to MySQL or die 
// and display an error. 
$link = mysql_connect($host, $username, $password); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 

// Select your database 
mysql_select_db ($database);         
     
   if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      // Temporary file name stored on the server 
      $tmpName  = $_FILES['image']['tmp_name'];   
        
      // Read the file 
      $fp      = fopen($tmpName, 'r'); 
      $data = fread($fp, filesize($tmpName)); 
      $data = addslashes($data); 
      fclose($fp); 
       
      $student_id=$row_student['student_id']; 

      // Create the query and insert 
      // into our database. 
      $query = "UPDATE student SET student_img='$data' WHERE student_id ='$student_id'"; 
      $query .= "(image) VALUES ('$data')"; 
      $results = mysql_query($query, $link); 
       
      // Print results 
      print "Thank you, your file has been uploaded."; 
       
} 
else { 
   print "No image selected/uploaded"; 
} 
[syntax=php]
// Close our MySQL Link
mysql_close($link);
?>

<form action="" method="post" enctype="multipart/form-data" name="changer">

<strong style="color: #FFD700;">Upload your image:</strong><br />
<input name="MAX_FILE_SIZE" value="102400" type="hidden"><br /><br />
<input namge="image" accept="image/jpeg" type="file">
<input type="submit" value="Submit">
</form>
[/syntax]

Code to display image:

Code: Select all

<?php 

$username = "MentorMenteeData"; 
$password = "mentormenteedata"; 
$host = "localhost"; 
$database = "mentormenteesystem"; 

mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error()); 

mysql_select_db($database) or die("Can not select the database: ".mysql_error()); 

$id = $_REQUEST['student_id']; 

if(!isset($id) || empty($id) || !is_int($id)){ 
     die("Please select your image!"); 
}else{ 

$query = mysql_query("SELECT * FROM student WHERE student_id='".$id."'"); 
$row = mysql_fetch_array($query); 
$content = $row['image']; 
} 

header('Content-type: image/jpeg'); 
     echo $content; 
?>
I could see my database table for the image column containing some bits but I just cant seems to display it. Please advise.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Updating Images (BLOB) in MySQL with PHP

Post by flying_circus »

What do you see when you try to display it? Are you getting a random looking string?

My initial thought it to question your design decision. Typically images are stored on the hard disk and a file path is recorded in your database. Is there a reason you are storing your images in a database?
whiterainbow
Forum Newbie
Posts: 11
Joined: Fri Mar 18, 2011 9:13 am

Re: Updating Images (BLOB) in MySQL with PHP

Post by whiterainbow »

I think flyingcircus is absolutely right to question your design, but you cannot just 'display' a string and expect PHP to know it's an image (rather than any other sequence of binary data. If storing the data in binary is the right approach and you still need to recode it for display, you'll need to use imagecreatefromstring() and probably at least one of the other php image functions.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Updating Images (BLOB) in MySQL with PHP

Post by mikosiko »

whiterainbow wrote:.... you'll need to use imagecreatefromstring() and probably at least one of the other php image functions.
or use the proper header('Content-type: ....') for the image before trying to display it.

In any case +1 with flyingcircus comment

Edit: Just saw that the OP is using the right header('Content-type...') on his code, therefore the question are... what data type is your column "image"?... it really contain a jpeg image?
Post Reply