SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Storing and Pulling Images from MySQL

    Hi, I'm using PHP plus MySQL to make a news page. I want to be able to update my images and news via a form, I already worked out the text part but I don't know how to store images in MySQL, I've heard something about BLOB type, but don't know exactly how does it work? Could you please help me or give me some links where I can learn about it?

    Thanks

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A BLOB field is used for storing binary data, this is what you would use to store images in your database. However I would take a different approach as storing images in a database is not needed, I would store the image name in the database but store the actual image in a folder somewhere on the server. This way you are not calling large amounts of data from the database everytime you want to display an image. For instance if you had a folder called images and you stored all your uploaded images there. You could simply store the image name in the db something like image001.gif. Now when retrieving the data frtom the db you get the image name and you can simply make an <img> tag in html with the image name since you already know the path.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What I'm trying to do is...

    updating all fields in a single form.

    Fields:

    articles.Title
    articles.Text
    articles.CID (category id) [dropdown list from Category table]
    articles.AID (author id) [dropdown list from Author table]
    articles.PID (priority id) [dropdown list from Priority table]
    articles.url1 (see also link 1)
    articles.url2 (see also link 2)
    articles.url3 (see also link 3)
    articles.image (the specific news image)

    Some articles don't have images. The main purpose of setting it all in one form is to make sure someone with knowledge zero to one, can update the content. The way you suggest would need the use of an alternative way to upload the images. Besides, how do I store the name of the image? Maybe I would need to set fixed image names? Thanks for your help.

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you do want to store the images in the database this article will help:

    http://www.phpbuilder.com/columns/florian19991014.php3

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well easy you add a field of type file to your form and allow the user to upload an image from their own computer which gets copied to a location on the server specified by your script, then you can get the name of the image from the uploaded file and just store that name in the database.

    Your form needs to have a file input and an enctype attribute in the form tag

    PHP Code:
    <form action="" enctype="multipart/form-data">

    <
    input type="file" name="userfile"

    That right there would allow you have the required fields to make this work. Once the file is uploaded your script gets three new vars automatically.

    $userfile_name
    $userfile_type
    $userfile_size

    Or whatever you called the file field in this case $userfile. So right there you have the file name $userfile_name, now you just need to copy() the file to its new home.

    http://www.php.net/copy

    Basically you nee to have a folder on the server tyo store the images and it needs 777 permissions to allow the web server to write to it. Then you can simply copy the uploaded file there.

    PHP Code:
    copy($userfile"path/to/new/folder/for/images/".$userfile_name); 

    This example is very basic and has no error checking, you'll need to customize it to work for you. There are some tutorials on how to do this.

    Here is one but it might bne overkill for your needs:
    http://www.phpbuilder.com/columns/bealers20000904.php3

    Another better one.
    http://www.devshed.com/Server_Side/PHP/User/
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks a lot

    Say hello to Freddy


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
  •