Page 1 of 1
Pictures in Mysql
Posted: Sun Mar 25, 2007 5:57 pm
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
Posted: Mon Mar 26, 2007 1:36 am
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.
Posted: Mon Mar 26, 2007 3:41 am
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.
Posted: Mon Mar 26, 2007 3:46 am
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.
Posted: Mon Mar 26, 2007 3:51 am
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.
Posted: Mon Mar 26, 2007 4:15 am
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?
Posted: Mon Mar 26, 2007 5:04 am
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.
Posted: Tue Mar 27, 2007 9:35 am
by Bandy
Thanks a lot.
When i finish my simple CMS ill try to make a tutorial for devnetwork forum.
Thanks a lot