Pictures in Mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Bandy
Forum Newbie
Posts: 22
Joined: Sat Oct 21, 2006 5:14 am
Location: Croatia

Pictures in Mysql

Post by Bandy »

Hi

Ive done some coding of simple CMS, and my Administration form have option to upload a jpg/jpeg picture.

If i upload picture for text what i write it should be related with that text.
So i done simple database that looks like this:
NEWS (ID(PK), title, content, contact, timestamp, picture);
Ok! Now i write upload code and then i have:

Code: Select all

imagejpeg($i,"pictures/uploadedfile.jpg",80);
So if i insert path like this in my query i will have same pic for every content i write.
Can i make that pic have the same number like news ID,
pictures/1.jpg
pictures/2.jpg
pictures/3.jpg
pictures/4.jpg
etc...
Or is there any different solution !?

Thx
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

You would need to insert the record without the file/path, use mysql_insert_id to get last inserted ID, then run an update on that record putting in the file/path with that ID.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Personally, I wouldn't rename images like that. I used to because it was the "best" solution, but your website ends up having lots of assets with meaningless names. These days I retain the original filename and check it doesn't clash with an existing file.If I'm rescaling it I insert 'thumbnail' or 'mid' or something between the name and the extension. It's all done with strpos to find the . and substr to break the name up to add stuff.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I'd store the filename in the database, but I'd store the image itself in the filesystem with the name of it's md5() hash. No destructive collisions, and you get to keep your original filename (for whatever reason). Also you can store relationship data in the database as well, like indicating that one file is the thumbnail of another - a much cleaner solution IMO.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Kieran Huggins wrote:I'd store the filename in the database, but I'd store the image itself in the filesystem with the name of it's md5() hash. No destructive collisions, and you get to keep your original filename (for whatever reason). Also you can store relationship data in the database as well, like indicating that one file is the thumbnail of another - a much cleaner solution IMO.
So you serve images called 123nkjn98ds7vkjn54kj4n35jkn.jpg? That's nasty.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You can use a context, as well as a token: like blog posts with the URL http://blog.zzz/posts/title-of-post

think: http://site.zzz/images/title-of-post/inline/1

or: http://site.zzz/images/skiing-ostrich/thumbnail

Why should images be any different from other posted content?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Ah, you didn't say you're rewriting the URLs. That's not so bad. So long as the user doesn't see md5().jpg as a filename.
User avatar
Bandy
Forum Newbie
Posts: 22
Joined: Sat Oct 21, 2006 5:14 am
Location: Croatia

Post by Bandy »

Thanks a lot.

When i finish my simple CMS ill try to make a tutorial for devnetwork forum.

Thanks a lot
Post Reply