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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Inserting image into database problem ??

    Hello forums !!
    I used the following code to upload the image in the DB
    PHP Code:
    $tmp_name     $_FILES['file']['tmp_name'];
            
    $filesize     filesize($tmp_name);
            
    $filename     $_FILES['file']['name'];
            
    $file_type     $_FILES['file']['type'];
            
    $contents     fread(fopen($tmp_name,'rb'),$filesize);
            
    //fclose($tmp_name);
            //echo $contents;
            //$contents = base64_encode($contents);    
            
    $addFile "INSERT INTO mantis_bug_file_table
                       (bug_id,filename,filesize,file_type,date_added,content)
                       VALUES
                       ('$idbug','$filename','$filesize','$file_type',NOW(),'$contents')"
    ;
            
    //echo $add5;
            
    $resultFile mysql_query($addFile) or die(mysql_error()); 
    when i run this code , apache got down and nothing worked.
    when i removed the $contents from the query or encoded the $contents with base64_encode() then it went right.
    What's the problem with reading the upload image and inserting it in the database or this is not the way of inserting image into database ?
    Any Idea ? Please Help
    Thanks in advance to all of you !

  • #2
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    have you set the column 'content' to a blob column?

  • #3
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Anyway i got the solution
    what i did ?
    i just placed the addslashes() in the $contents and inserted.and it worked...
    Now One more Question ?
    How to retrive the image from database ?
    Thanks in advance to all of you !

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You need to make another file to output the image so you can link to it on your main page with the <img src="image.php?id=123" /> tag.

  • #5
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try to use my code in inserting image in the database...

    unset($imagename);

    if(!isset($_FILES) && isset($HTTP_POST_FILES))
    $_FILES = $HTTP_POST_FILES;

    if(!isset($_FILES['n_pic']))
    $error["n_pic"] = "An image was not found.";

    $imagename = basename($_FILES['n_pic']['name']);

    if(empty($imagename))
    $error["imagename"] = "The name of the image was not found.";

    if(empty($error)) {
    $newimage = "Images/Articles/" . $imagename;
    $result = @move_uploaded_file($_FILES['n_pic']['tmp_name'], $newimage);

    if(empty($result))
    $error["result"] = "There was an error moving the uploaded file.";
    }
    $n_pic = $newimage;

    Just try it....Thats what I used in inserting image into db.


  •  

    Posting Permissions

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