SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Kuching, Sarawak
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating Images(BLOB) in MySQL with PHP

    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:
    PHP Code:
    <?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($fpfilesize($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"


    // 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>
    Code to display image:
    PHP Code:
    <?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.

  2. #2
    SitePoint Member
    Join Date
    Feb 2012
    Location
    Switzerland
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    only thing I can see is the adslashes, that is not removed on out put - stripslashes..
    but not sure if that is even related..

    about 8 years a go, I read a book where it was clearly explained "Images do not belong in DBs"
    I do agree with that assesment and never doubted it..

    Are you sure you want to do that?
    it creates dozens of problems an I dont see a single benefit..

    My general advice:
    files in file system - data in databases ..

    regards
    Hensel

  3. #3
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    loading and retreiving images to sqli db with php

    I have been trying to do similar and the images only display as text with the following code:

    <?php
    $id = $_POST['id'];
    $_POST['display_blob'] = true;

    $dbc = mysqli_connect('localhost', '-------', '--------', '--------')
    or die('Error connecting to MySQL server.');
    $query ="SELECT image FROM tbl_images WHERE id = $id";
    $result = mysqli_query($dbc, $query)
    or die('Error querying database.');
    //header("Content-type: image/jpeg");
    $row = mysqli_fetch_array($result);
    echo $row['image']; //retreives ascii

    // header("Content-type: image/jpeg"); //added to display as image did not work
    // echo $row['imageContent']; //added to display as image did not work

    mysqli_close($dbc);
    ?>

    I think that storing images in the db has it's place and there are plenty arguments to be found for doing such; however, the purpose of this thread is how and not why.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •