homepage Welcome to WebmasterWorld Guest from 54.145.172.149
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Storing a picture in mysql with php
Is there an easy way?
dhdweb

10+ Year Member



 
Msg#: 754 posted 10:45 pm on Feb 22, 2003 (gmt 0)

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.

 

brotherhood of LAN

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



 
Msg#: 754 posted 10:50 pm on Feb 22, 2003 (gmt 0)

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.

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 754 posted 10:51 pm on Feb 22, 2003 (gmt 0)

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

dhdweb

10+ Year Member



 
Msg#: 754 posted 11:54 pm on Feb 22, 2003 (gmt 0)

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?

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 754 posted 12:04 am on Feb 23, 2003 (gmt 0)

>>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

dhdweb

10+ Year Member



 
Msg#: 754 posted 12:15 am on Feb 23, 2003 (gmt 0)

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

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 754 posted 12:30 am on Feb 23, 2003 (gmt 0)

>>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

dhdweb

10+ Year Member



 
Msg#: 754 posted 2:48 pm on Feb 23, 2003 (gmt 0)

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

lorax

WebmasterWorld Administrator lorax us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 754 posted 4:50 pm on Feb 23, 2003 (gmt 0)

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.

hakre

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 754 posted 4:58 pm on Feb 23, 2003 (gmt 0)

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.

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 754 posted 5:52 pm on Feb 23, 2003 (gmt 0)

>>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

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 754 posted 5:55 pm on Feb 23, 2003 (gmt 0)

>>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

globay

10+ Year Member



 
Msg#: 754 posted 9:00 pm on Feb 23, 2003 (gmt 0)

Wouldn't it be easier, if they just upload them, via a formular?

lorax

WebmasterWorld Administrator lorax us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 754 posted 2:04 pm on Feb 24, 2003 (gmt 0)

>> 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. :)

dhdweb

10+ Year Member



 
Msg#: 754 posted 11:58 pm on Feb 24, 2003 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved