Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    England
    Posts
    132
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Question Cannot Display Image Uploaded into MySql DB

    I've followed an online tutorial for storing an image into a MySQL database.

    I have created a table (databaseimage) with three fields id (primary key, auto increment), name, image (mediumblob type).

    The php is supposed to display the image after it has been uploaded but I get the problem uploading image from the if then else statement. Also the full image name including the name of the tmp dir is displayed but the php is only supposed to display the filename. Despite these issues I can see the images inside the database with the correct filenames and sizes!

    PHP Code:
    <html>
    <head>
    <title>Upload image</title>
    </head>

    <body>

    <form action="imageupload.php" method="post" enctype="multipart/form-data">
    File:
    <input type="file" name="image"> <input type="submit" value="upload">
    </form>

    <?php

    // connect to database

    mysql_connect("host""username""password") or die(mysql_error());
    mysql_select_db('dbname') or die(mysql_error());

    // file properties
    echo $file $_FILES['image']['tmp_name'];

    if (!isset(
    $file))
    echo 
    "Please select an image.";
    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's not an image.";
    else
    {
    if (
    $insert mysql_query("INSERT INTO databaseimage VALUES ('','$image_name','$image')"))
    echo 
    "Problem uploading image";
    else 
    {
    $lastid mysql_insert_id();
    echo 
    "Image uploaded<p />Your image:<p /><img src=get.php?id=$lastid>";
    }
    }
    }
    ?>

    </body>
    </html>
    PHP Code:
    <?php

    mysql_connect
    ("host""username""password") or die(mysql_error());
    mysql_select_db('dbname') or die(mysql_error());

    $id addslashes($_REQUEST['id']);

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

    header("Content-type: image/jpeg");

    echo 
    $image;

    ?>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,492
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Is there a reason why you want to store the images in the database (BLOB) and
    not store the images in a directory and use the table for storing the image filenames?

    In other words, do you really need to store the images/photos in your database table?


    .

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    England
    Posts
    132
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Is there a reason why you want to store the images in the database (BLOB) and
    not store the images in a directory and use the table for storing the image filenames?

    In other words, do you really need to store the images/photos in your database table?


    .
    I'd like to know how to do it before exploring storing the images into a dir which looks more complicated and the images will relate to an individual's record in the database.
    Last edited by Mehdi72; 01-06-2013 at 07:53 PM.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by mlseim View Post
    Is there a reason why you want to store the images in the database (BLOB) and
    not store the images in a directory and use the table for storing the image filenames?

    In other words, do you really need to store the images/photos in your database table?
    Believe it or not storing in a db can actually be a better idea. Say you delete a user from the DB and then their images from the disk. If you do a rollback, the users account can be restored but those images that were wiped from the system are gone for good!

    Of course then you have the problem of a swelling mysql table
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Location
    England
    Posts
    132
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Believe it or not storing in a db can actually be a better idea. Say you delete a user from the DB and then their images from the disk. If you do a rollback, the users account can be restored but those images that were wiped from the system are gone for good!

    Of course then you have the problem of a swelling mysql table
    My problem is I can't get this code to work correctly!

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    To save an image to a database you should use SQL to read the file into the field like this:

    Code:
    INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file))
    That way you do away with the PHP code to try to convert it from a file into data that can be inserted into the database. You can also use the SQL DUMPFILE command to write the field from the database directly into a file again. The file in the database should have all of the headers etc included with it (as happens when you use LOAD_FILE) as otherwise the image can be easily broken.

    I assume the autoincrement field is there to allow for multiple images with the same name as otherwise the image name could be used as the primary key.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by Mehdi72 View Post
    My problem is I can't get this code to work correctly!
    £20 (thats UK pounds) by paypal and I'll fix it fo you via teamviewer
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    Regular Coder
    Join Date
    Jul 2007
    Location
    England
    Posts
    132
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    To save an image to a database you should use SQL to read the file into the field like this:

    Code:
    INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file))
    That way you do away with the PHP code to try to convert it from a file into data that can be inserted into the database. You can also use the SQL DUMPFILE command to write the field from the database directly into a file again. The file in the database should have all of the headers etc included with it (as happens when you use LOAD_FILE) as otherwise the image can be easily broken.

    I assume the autoincrement field is there to allow for multiple images with the same name as otherwise the image name could be used as the primary key.

    Parse error: syntax error, unexpected T_STRING in imageupload.php on line 36

    Code:
    if ($image_size==FALSE)
    echo "That's not an image.";
    else
    {
    if ($insert = mysql_query("INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file)))
    echo "Problem uploading image";
    else
    Don't I need to declare the file variable?

  • #9
    Regular Coder
    Join Date
    Jul 2007
    Location
    England
    Posts
    132
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    £20 (thats UK pounds) by paypal and I'll fix it fo you via teamviewer
    Thanks for the offer but this isn't work related. I'm not going to learn php if I pay to get problems fixed!

  • #10
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,300
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Quote Originally Posted by Mehdi72 View Post
    Parse error: syntax error, unexpected T_STRING in imageupload.php on line 36

    Code:
    if ($image_size==FALSE)
    echo "That's not an image.";
    else
    {
    if ($insert = mysql_query("INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file)))
    echo "Problem uploading image";
    else
    Don't I need to declare the file variable?
    That you do and you need to fix the syntax of the snippet you posted. Instead of using code tags use PHP tags and it'll become pretty obvious what is wrong a good portion of the time.

    PHP Code:
    if ($image_size==FALSE) {
         echo 
    "That's not an image.";
    } elseif (
    $insert mysql_query("INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file)")) {
         echo 
    "Problem uploading image";
    } else {
    // do something else here if you need to

    Dave .... HostMonster for all of your hosting needs


  •  

    Tags for this Thread

    Posting Permissions

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