Welcome to WebmasterWorld Guest from 54.162.155.183

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Storing a picture in mysql with php

Is there an easy way?

     
10:45 pm on Feb 22, 2003 (gmt 0)

10+ Year Member



I would like to have users be able to upload a number of pictures to my db so that I can download them to my computer. Most of the users I deal with would not know how to ftp them to my server so I want to create a form that makes it idiot proof. (they currently have to e-mail them to me)

I can handle making the form and such, I just need to know how to get the pictures into the db useing php.

Thanks for any help you can provide.

10:50 pm on Feb 22, 2003 (gmt 0)

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Most seem to just store the path to the image, rather than try to store the image itself (im not sure if you can store images, but i guess if you can store binary in mysql you can store anything)

storing all the images in one folder and saving the filename as the primary key would be the way id try it.

10:51 pm on Feb 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is nothing special about inserting an image into a BLOB [mysql.com] column. You just fopen() [php.net] the file, fread() [php.net] the file, use mysql_escape_string() [php.net] to escape any funny characters and the INSERT [mysql.com] it into the db using your usual mysql_query() [php.net].


$fp = fopen("s:/title.gif", "rb");
$content = mysql_escape_string(fread($fp, filesize("s:/title.gif")));
$y = mysql_query("INSERT INTO table SET content='$content'");

HTH Andreas

11:54 pm on Feb 22, 2003 (gmt 0)

10+ Year Member



Please forgive me, I have a long way to go to master php!

$fp = fopen("s:/title.gif", "rb");

What is the "rb"?

$y = mysql_query("INSERT INTO table SET content='$content'");

What is the var $y for?
12:04 am on Feb 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>What is the "rb"?

This is explained in the PHP [php.net] manual entry for fopen() [php.net]: r = read, b = binary.

>>What is the var $y for?

This is explained in the PHP [php.net] manual entry for mysql_query() [php.net]: mysql_query() [php.net] returns TRUE on success and FALSE on error. It is a good idea to always check for success of an operation.

Please read the manual entries for the functions you are using. Iīm making this really easy by providing the links, so there is really no excuse not to spend a little time exploring on your own. This will help a lot and while it may take some time at first you will benefit from it in the long run.

HTH Andreas

12:15 am on Feb 23, 2003 (gmt 0)

10+ Year Member



Many thanks andreasfriedrich!

Yea yea, I know ..... RTFM :)

I'm now making yet another trip to php.net for more reading!

(sometimes I just don't get their function explanations until I see a code sample that make sense!)

dhdweb

12:30 am on Feb 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>sometimes I just don't get their function explanations
>>until I see a code sample that make sense!

Thatīs alright and perfectly understandable. And whenever you have a question do feel free to post here.

Itīs just that r = read is not one of those instances where the PHP [php.net] manual is hard to understand ;).

Andreas

2:48 pm on Feb 23, 2003 (gmt 0)

10+ Year Member



Itīs just that r = read is not one of those instances where the PHP manual is hard to understand ;).

You got me on that one, I somehow scanned right over that!

I hate it when you can't see something that is right in front of your face, maybe I need new glasses? :)

Thanks again andreas!

dhdweb

4:50 pm on Feb 23, 2003 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



FWIW I'm in favor of BOL's approach unless you absolutely need to use a BLOB field. I'm not aware of any advantage of using BLOB - no smarty pants remarks Andreas ;) - over a simple VARCHAR containing the path.
4:58 pm on Feb 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



like lorax said, there might be even a big disadvantage by using blobs:

if your webserver uses cgi for your scripts, then there might be a cgi-limit. if every picture has to be displayed out of a script, then on a listing with 20 entries, you'll get 20 extra cgi spawns only for the pictures. this can heavily stress your webserver and the limit can be reached quite fast. then only broken images would be displayed.

on the other hand, it's easier to delete an item, because it's in the db only, not in the filesystem, too.

5:52 pm on Feb 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>I'm not aware of any advantage of using BLOB [...]
>>over a simple VARCHAR containing the path.

I did not try to advocate either ;).

In fact I sometimes do neither, that is I do not store the binary data in a BLOB [mysql.com] nor do I store the path of the binary file in the db. What I do is name the file after the value of the PRIMARY KEY [mysql.com] of the db.

Andreas

5:55 pm on Feb 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>if your webserver uses cgi

If your host provides just the PHP [php.net] binary instead of the module then Iīd switch hosts ;).

Andreas

9:00 pm on Feb 23, 2003 (gmt 0)

10+ Year Member



Wouldn't it be easier, if they just upload them, via a formular?
2:04 pm on Feb 24, 2003 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



>> What I do is name the file after the value of the PRIMARY KEY of the db.

That's another nugget I hadn't thought of. Thanks. :)

11:58 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



To be honest, I scrapped this idea in favor of creating a PHP form that creates a new directory and allows the user to upload 5 pictures at a time. (much easier)

I should have thought of this in the first place since all I really wanted was an easy way for users to send me thier pics with out having to use e-mail or FTP.

Talk about having a brain fart! :)

Now, once they have created the new dir and uploaded the pics, I just FTP them down to my computer.

Thanks again for the info for the db, I may need that someday.

dhdweb

 

Featured Threads

Hot Threads This Week

Hot Threads This Month