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 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Storing Images in DB

    Hi, I was wanting to store images inside a database but I have gotten stuck in doing so.

    So bacically, i have a

    Code:
    <input type="file" name="pic1">
    then, that is suubmitted in a form to another php page. In that, i use

    Code:
    $pic1 = $_POST['pic1'];
    $pic1 = addslashes(fread(fopen($pic1, "r")));
    However, this method doesn't seem to be working.

    Can anyone help me with this method or does anyone know of a good tutorial on exactly how this can be achieved?

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    if you are wanting to store images in a DB then id suggest using a HTML form with the action of uploading the file to your webserver and then storing the image name along with an ID in a DB using MySQL.

    if this isnt quite what your trying to do could u please elaborate on the question a little more so we can help you out.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Location
    Pakistan
    Posts
    207
    Thanks
    0
    Thanked 0 Times in 0 Posts
    we dont store image itself in database, we store path of image in database.
    here is the complete working code.

    /*****HTML Portion*****/
    <form action="urform.php" method="post" enctype="multipart/form-data"> Image <input type="file" size="22" name="image">
    <input type="submit" value="Add" name="add">
    </form>
    /*******************/

    PHP Code:
    $pimage $_FILES['image']['name'];    
    $name $_POST['name'];

    copy($_FILES['image']['tmp_name'],"images/$pimage");  //copy image into folder 'images'

    $query="INSERT INTO urtable (urfield) values ('$pimage')";  //into database

    echo "<img src='images/".$pictures[$i]['productimage']."'height='100' width='125'>";  // to show image 
    Now change it according to ur requirements

    Regards,
    Aamir.
    God helps those who help others.

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To access an uploaded file, you use $_FILES['pic1'] rather than $_POST['pic1']. Also, make sure the correct enctype is set in the form or you may have problems uploading:
    PHP Code:
    <form name="myform" enctype="multipart/form-data" method="post" action="script.php"
    $_FILES has the following elements you can then access:
    PHP Code:
    $_FILES['userfile']['name'// the original name of the file
    $_FILES['userfile']['type'// eg image/jpeg
    $_FILES['userfile']['size'// filesize
    $_FILES['userfile']['tmp_name'// the tmp name of the uploaded file on the server
    $_FILES['userfile']['error'// an error code - 0 means no error, 4 means no file uploaded, etc 
    I can't test it directly but I think you should be able to do:
    PHP Code:
    $pic1 addslashes(fread(fopen($_FILES['userfile']['tmp_name'], "r"))); 
    Alternatively you may need to move the file to another directory where you an access it, upload it to the database then unlink() the file on the server (you might need to do this if you experience open_basedir errors), you can move the file using move_uploaded_file().

    This section on handling file uploads on php.net is very useful as is the page on error codes.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    paris, france
    Posts
    1,216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Amir, You can store images in databases if you want sing the blob column type. must be a bandwidth eater though.
    photoshop too expensive? use the GIMP! www.gimp.org

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whackaxe
    Amir, You can store images in databases if you want sing the blob column type. must be a bandwidth eater though.
    If the database is local then it shouldn't really require any extra bandwidth whether the images are on the server or in the database. What it will do is use extra resources to store and retrieve the data from the database but this is likely to be minimal. Even so, the only real reason to do this is to restrict access to files or to aid in creating statistics for file downloads/views. If that's not your aim then it's simpler to just store the images as files on the server.

  • #7
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts


  •  

    Posting Permissions

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