Reputation: 17
In php if i have a image uploading option in my form then how can i store that image in Database and as per need how can i retrieve it.I have a form with some fields like Name Age Email etc. and one field is about user's image. So tell me how can i upload a picture and store it and retrieve it again in next page.Can i use just simple input tag in HTML code and declare type as file.
<body>
<input type="file" accept="image/jpeg" />
<input type="submit" value="Submit Form">
</body>
Upvotes: 0
Views: 184
Reputation: 7597
<form>
.Example:
<body>
<form action='index.php' method='POST' enctype='multipart/form-data'>
<input type="file" name="img" accept="image/jpeg" />
<input type="submit" name="submit_button" value="Submit Form" />
</form>
</body>
You have to use $_FILES, then upload it with move_uploaded_file()
.
PS: Why do you want to upload it to DB? Why not saving to the server?
You should store it on the server, and write path to DB:
if (is_uploaded_file($_FILES['img']['tmp_name'])) {
move_uploaded_file($_FILES['img']['tmp_name'], 'some_folder_with_images/'.$_FILES['img']['name']);
}
And then this will be your query (example), and of course you should escape file name, or just generate your own file name of stored file on the server:
$query = "INSERT INTO images (image_path)
VALUES ('".$_FILES['img']['name']."')";
PS: use mysqli_*
, mysql_*
is deprecated.
Upvotes: 1
Reputation: 8809
Sunil, if you want to insert/upload image into database than you have to use blob field type to insert image.
I feel you should go through with this http://vikasmahajan.wordpress.com/2010/07/07/inserting-and-displaying-images-in-mysql-using-php/ article to add and retrieve image/picture to db.
Upvotes: 1