Uploading and retrieving image from mysql : How to ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
azhan
Forum Commoner
Posts: 68
Joined: Fri Jun 27, 2008 6:05 am

Uploading and retrieving image from mysql : How to ?

Post by azhan »

Hey guys....i've found a complete coding for uploading files such as image from mysql using php script...but the problem here is..when i wanted to see whether the data had been successfully uploaded into my database...my table of images return "empty"..but my php return successfully uploaded which means successfully stored in the database....why does this happen? does the <input type = "hidden" > in the upload form is the cause?

Below are the codes:

Code: Select all

<html>
<head><title>Store binary data into SQL Database</title></head>
<body>
 
<?php
// code that will be executed if the form has been submitted:
 
if ($submit) {
 
    // connect to the database
    // (you may have to adjust the hostname,username or password)
 
    MYSQL_CONNECT("localhost","root","paswword");
    mysql_select_db("database_name");
 
    $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
 
    $result=MYSQL_QUERY("INSERT INTO images (description,bin_data,filename,filesize,filetype) ".
        "VALUES('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
 
    $id= mysql_insert_id();
    print "<p>This file has the following Database ID: <b>$id</b>";
 
    MYSQL_CLOSE();
 
} else {
 
    // else show the form to submit new data:
?>
 
    <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="form_description"  size="40">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload/store in database:<br>
    <input type="file" name="form_data"  size="40">
    <p><input type="submit" name="submit" value="submit">
    </form>
 
<?php
 
}
 
 
?>
 
</body>
</html>
Image

Another thing is...how to retrieve or display the image back to php page??

Thanks

Azhan
________________
http://www.productcoverdesign.com- " Cheapest E-cover Design"
Last edited by azhan on Wed Jul 02, 2008 6:42 am, edited 1 time in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Uploading and retrieving image from mysql : How to ?

Post by Eran »

I will recommend against saving the images in your database, it is very inefficient. Save them on the regular file-system and only store the path and filename in the database.
azhan
Forum Commoner
Posts: 68
Joined: Fri Jun 27, 2008 6:05 am

Re: Uploading and retrieving image from mysql : How to ?

Post by azhan »

hurmm..and how would i do that?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Uploading and retrieving image from mysql : How to ?

Post by Eran »

Check out this tutorial - http://www.php.net/manual/en/features.file-upload.php
After moving the image to the directory you want to store it in, save the image filename in the database for future reference.
Post Reply