SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how could I insert a image into a database from a upload tag? I would like to upload an image into a database, get that image, and display it to my viewers. It's a online reference, and I would like to be able to add a new row (each one has 1 pic) without having to add a picture in another folder, etc.

    Also, if I do this, how can I backup my database? Currently I'm just using PHPMyAdmin to "send" the backup info to my harddrive in a *.sql file...
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Search for the words "binary data" on PHPBuilder.com, they have a fine article on storing things like images in a MySQL database.

    Overall, though, I would recommend simply copying the uploaded file to a special location as opposed to storing it in the actual database...you could copy it, and then simply store a link to it's location in the DB.

  3. #3
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is this the preferred method? I mean, I could just store it in a folder on my server, and store the URL of it in my database... but I would prefer to do it this way... thanx though, I'm going to check out the site you gave me!
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that is the way I do it, using a function (I belive it is copy(), or something like that), you can take the uploaded file and copy it to any local destination you like.

  5. #5
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For my site, I keep uploaded images in their own directory and thus, outside of the mysql table. I simply rename the image to the ID of the new entry in the table (containing the name, email, etc).

    Seems to work fine.

  6. #6
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    robp,

    could you please provide me an example? I'm really lost on this one! I like the idea of the ID and the image name... could you please provide an explanation, and mabey some code?

    thanx,

    Alex
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a snippet of code from something I am writing now:
    PHP Code:
    switch ($action) {
        case 
    "addNew":
            for (
    $i=0$i<2$i++) {
                if (
    $userFile_size[$i] > 0) {
                    
    $userFile_name[$i] = "_" $i "_" $userFile_name[$i];
                    
    copy($userFile[$i], $DOCUMENT_ROOT ."/images/" $userFile_name[$i]);
                }
            }
            
    $sqlStr "INSERT INTO item SET name='$name', short_description='$short', long_description='$long', small_image_name='" 
                
    $userFile_name[0] . "', large_image_name='" $userFile_name[1] . "'";
            
    mysql_query($sqlStr$dbconx) or die( mysql_errno() . " : " mysql_error() );
            
            
    $id mysql_insert_id($dbconx);
            
            foreach(
    $categories as $cat) {
                
    $sqlStr "INSERT INTO item_category SET item_id='$id', category='$cat'";
                
    mysql_query($sqlStr$dbconx) or die( mysql_errno() . " : " mysql_error() );
            }
            break;

       ... 
    copy($userFile[$i], $DOCUMENT_ROOT ."/images/" . $userFile_name[$i]);

    This is the pertinent line. It saves the file to where I want it. you could rearrange the logic to first update the database, then retrieve the last auto_increment and use it as the file name in the second arguement in copy().

    The manual is worth reading on this topic:
    http://www.php.net/manual/en/features.file-upload.php

  8. #8
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you all. I think that I'll be able to get it now! God Bless all of you for your time and help!
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  9. #9
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In case you are still wondering, here is my example:
    PHP Code:
    //table: |ID|Name|Email|
    //inserting the new data
    mysql_query("INSERT INTO $mysql_table VALUES(null,'$name','$email')") or die(mysql_error_msg());
    //copying the uploaded image to the specified directory, and renaming it to the ID of the entry that it goes with in the database
    copy($image"/home/mullet/www/images/".mysql_insert_id().".jpg") or die("ERROR COPYING"); 
    Of course, ID would have to be an AUTO_INCREMENT column.
    Hope that helps.

  10. #10
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you robp! That was a simpler way than the rest! I much prefer to start out simple and build my own stuff.. then I know how it works, it's not somebody elses code. Thank you again!
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services


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
  •