Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,868 members, 7,817,565 topics. Date: Saturday, 04 May 2024 at 02:30 PM

How To Upload An Image Into Mysql Database - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / How To Upload An Image Into Mysql Database (2108 Views)

How To Upload A Song(your Song) Or Other Application Or File To Waptrick / Help, Cant Insert Data Into MYSQL Database Table / How To Upload Mysql Database To Server (2) (3) (4)

(1) (Reply) (Go Down)

How To Upload An Image Into Mysql Database by otentic19: 8:05am On Jan 19, 2012
It is quite easy to store text like username and passwrd into mysql database.The problem i am having is how to upload an image or pdf or any other files into mysql.Your contributions will be welcomed.
Regards.
Re: How To Upload An Image Into Mysql Database by teemy(m): 10:27am On Jan 19, 2012
You do not upload your file to db but rather the link to your file. The file in question could be moved to a 'files' folder.
Re: How To Upload An Image Into Mysql Database by yawatide(f): 11:30am On Jan 19, 2012
teemy:

Weelll, you could upload to a database table by declaring the column as a BLOB but it wouldn't be the best thing to do wink

So, OP, as teemy as said, save the path to the image in your DB and save the image in that path. You will be saving a ton of storage space when you do that cool
Re: How To Upload An Image Into Mysql Database by teemy(m): 11:46am On Jan 19, 2012
@yawa, I've never used blob data types as I prefer to stick with the ones me mind goes with. But, we never can tell maybe someday but hopefully not.
Re: How To Upload An Image Into Mysql Database by otentic19: 2:59pm On Jan 19, 2012
@teemy can u explain the php code that will process the image, i.e store it in a folder and save the path in mysql
Re: How To Upload An Image Into Mysql Database by teemy(m): 4:06pm On Jan 19, 2012
On the road, maybe when I power up my PC
Re: How To Upload An Image Into Mysql Database by Afam4eva(m): 4:31pm On Jan 19, 2012
Those blob and other media data types are just theory for me. I've never even bothered to try them for once. I use what teemy sugested, which is storing the link that links to the file.
Re: How To Upload An Image Into Mysql Database by Nobody: 4:48pm On Jan 19, 2012
*Passing by* Glad to see guys up to the task. I am still trying to finish up some rather tough tasks here.

Step 1 - upload the image with normal upload code for php.
Step 2 - Get the name used to store the image on the server
Step 3 - Store the name of the image unto the dbase

Am sorry, i cant be more detailed than this. He he

*Zooms off at break-neck speed*
Re: How To Upload An Image Into Mysql Database by yawatide(f): 5:23pm On Jan 19, 2012
teemy:
The issue wasn't whether we have or haven't used it but that it can be done. That is where I was coming from. Whether or not it is efficient is another issue for another day wink
Re: How To Upload An Image Into Mysql Database by teemy(m): 5:52pm On Jan 19, 2012
@Yawa, you know se I dey fear you right from day one. I no want your yawa o. Just kidding. Anyways, I am just anti-blob biased same way some bro would not look twice at a trousered lady.
Re: How To Upload An Image Into Mysql Database by yawatide(f): 6:49pm On Jan 19, 2012
teemy:

Mo o ni yawa o, he he. Again, you miss the point.

what IS NOT my point? Whether or not BLOB is good or bad. IMHO, it is not good.

What IS the point? Whether it is possible to store raw images in a database. Fact: It is possible. Having said that, refer to my comments in "what IS NOT my point?"

Thanks
Re: How To Upload An Image Into Mysql Database by teemy(m): 7:11pm On Jan 19, 2012
@yawa Ohhhhh! I refuse to fall in love with blob even if she is the finest chick on the block. She has 'toasted' me several times I no gree. Na me be dat for you. Just my two dimes dear.
Re: How To Upload An Image Into Mysql Database by teemy(m): 7:15pm On Jan 19, 2012
@yawa Jokes apart, do we drop codes for the guy or grill him as well
Re: How To Upload An Image Into Mysql Database by teemy(m): 7:35pm On Jan 19, 2012
As I have never used blob, you could example with that format.
Re: How To Upload An Image Into Mysql Database by yawatide(f): 7:38pm On Jan 19, 2012
Nope,

dhtml has answered him already. Besides, OP says it is very easy to store username and password. Storing the path to the image should equally be as easy, right? tongue
Re: How To Upload An Image Into Mysql Database by teemy(m): 7:46pm On Jan 19, 2012
I agree with you as long as he has no kasala with the FILE array elements while uploading. Let's hope he gets the move_uploaded_file function right.
Re: How To Upload An Image Into Mysql Database by Nobody: 9:14pm On Jan 19, 2012
Yeah, i should add something else here. One of the problems you might face is that what happens when different people uploads files with the same names?

So here is what i do.

I have a table like this.

tablename: files
fields: ID (autoincrement primary key), filename (the original name of the file e.g. babe.jpg), storename (name generated e.g. with md5 hash or mt_rand)
That name for storage will be generated and stored in the database, this is the name that will be used to store the file in the file system.

So, if you upload say 2 files called babe.jpg.
the first one can be stored as 35r34fgdfd.file, and the other as cfgdsgdx.file
but when you read the table, you can get the original name of the file, so when you want to render the file to the browser, you can use the original name to render it:


header('Content-type: application/text');
header('Content-Disposition: attachment; filename="'.$title.'.txt"');
Around the filename area, you grab? I just quickly copied the code i used inside www.textpad..net for you. Something like that to render an image that is read from the database.

*Zooms off again* If you need clarifications, please ask, am not sure of what you know and what you dont.
Re: How To Upload An Image Into Mysql Database by teemy(m): 2:25pm On Apr 13, 2012
Possibly if you have a membership site with unique usernames or userids, you could save the name of the picture to something like usernamepassport.jpg
Re: How To Upload An Image Into Mysql Database by Adesege(m): 7:51pm On Sep 22, 2015
Old threw but my contribution maybe useful to other people who want to solve a problem like this.

It's not advisable to store an image as a blob type in a db, performance wise.

To go about the question OP asked, the following steps should guide you right:

1. You need to know that images will not be saved in a db, so you will need a storage to store your files. That means l, you will need to upload the image on your sever.

2. Once the image has been uploaded, you will need to store the image location or url in the database against the user who uploaded. You can have a table for the uploaded images with ID (auto increment and primary key), user_id, img_url. The user_id will be joined on the user table to get other information relating to the user. This is called a relational database. You can read up more of this on WIKI.

Basically, from the layman understanding, those are the two things that needs to be done but programmatically, you will still write thousands lines of codes.

I hope this helps someone.

@ yawatide, how u dey? Good to see ladies-in-programming

(1) (Reply)

How Much Should I Charge To Design A Website For A Hotel?!? / I Wanna Sell My Blog / Website Of Punch Newspaper Finally Hacked

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 23
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.