SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    VICTORY IS MINE! luchazard's Avatar
    Join Date
    May 2001
    Location
    USA
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can pictures be stored in mySQL?

    I have a site with jokes/pictures I have recently decided to put the tons of jokes/pictures I have into mySQL and PHP.

    I started by making the 'picture' database:

    CREATE TABLE pictures (
    ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    Title TEXT,
    Date DATE NOT NULL
    Vote INT
    );

    Now I will be having links to my pictures on my main page. The links will be like this: site.com/pictures/1.php etc.

    Now when the user follows the link the picture template page will appear, and the php part will call from the mySQL database: the picture, date, title, current average vote

    Now here is my question: Where will I store the actual picture? I know for the jokes I can store the joke's text in tin mySQL but what about storing pictures? Can pictures be stored in mySQL?

    Thanks in advance! As you can probably see from my post that I started learning PHP and mySQL a day or so ago
    Last edited by luchazard; May 26, 2001 at 09:42.

  2. #2
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is possible to put an image in a mySQL database using the BLOB (binary Large OBject) field type, but it's not at all recommended. It's very tricky getting the image in there, and it's a very inefficient way of using mySQL.

    A much better alternative is to have a directory on your webserver called something like /dbimages/ and to save image files in there, renaming the image to that it's file name matches the ID field in your database. For example, 1.jpg would be the file associated with record ID number 1 in the database table.

    To do this you need to figure out how to upload images via a web form. I've never done this personally but there are plenty of excellent tutorials on the subject. Once the image is uploaded simply use PHP file commands to copy the file into the directory and rename it to whatever.jpb

    http://blazonry.com/scripting/upload-size.html
    http://phpbuilder.com/columns/floria...14.php3?page=1

    Cheers,

    Skunk

  3. #3
    VICTORY IS MINE! luchazard's Avatar
    Join Date
    May 2001
    Location
    USA
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Skunk,

    Thanks for the advice I have some follow up questions now.

    To do this you need to figure out how to upload images via a web form.
    Why would I need to put the pictures in the 'dbpictures' from a web based form?

    Right now I do have a directory with over 100 pictures called 'p' (p instead of pictures so links are shorter) the pictures are labeled 1-100, so the first picture is /p/1.jpg second picture /p/2.jpg etc.

    The part I'm confused on now is making the pictures appear on the template page. So mysite.com/p/1.php the first picture will appear. Since I'm now not storing pictures in the mySQL database just the info about them... name, date etc. Since you advised me not to, I'm now stuck on how to make the pictures appear with php.

    Thanks!
    Last edited by luchazard; May 26, 2001 at 10:11.

  4. #4
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the images are already there you don't need to do anything You can also use FTP in the future to add more images.

    I suggested the web form because usually web sites are database powered to allow users to update the site using a password protected web form. By using a web form to upload the images as well you can add new images to the site without having to log in using FTP.

  5. #5
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Skunk,

    I'm trying to compare the pros and cons of storing images/documents in a MYSQL database (LONGBLOB field), as compared with storing them in one or many directories on the web server. Files will definitely be uploaded via a web page form (ie. not via ftp).

    The big problem I have with saving to a directory on the web server is that I have to CHMOD 777 the directory in order to allow this to happen. This creates a vulnerability on the shared/virtual hosting server I'm using that I'd rather not have. I agree that storing such files into a MYSQL database is not so efficient, but it appears that this then gives me more control/security over those files than the alternative.

    Comments?
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  6. #6
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...and if anyone wants to learn how to store and later access images in a MySQL database, try: http://www.phpbuilder.net/columns/florian19991014.php3
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise


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
  •