Reputation: 5561
Can u please guide me how to upload image in MySql databse through PHP. I am having only few days experience in PHP . I can go with any approach means saving file in directory and saving it's path in Database, or saving entire image in databse.
I will be thankful to u if u can provide some code or link to refer. I have already follow some links but those were buggy.
Upvotes: 0
Views: 1129
Reputation: 6602
This will insert the image in a directory, and image name will be stored in database hope this will help you for starting
<form name="aaa" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
//to add your own image name $name = $_FILES['image']['name']; $extension = strrchr($name,'.'); $image_name = "whatever_you_put".$extension;
move_uploaded_file($_POST['image']['tmp_name'],"/image/".$image_name) //where image is a directory where image will be uploaded
mysql_query("insert into table_name VALUES ('".$image_name."')");
}
Upvotes: 0
Reputation: 2312
You should use file_get_contents() function and insert result into BLOB field of your table
Upvotes: 0
Reputation: 64933
I'll suggest you to avoid that in MySQL, because this database engine lacks of a good SQL binary append aggregated function.
This means that if you're going to save large images to MySQL, you'll need to upload them in a single trip, which is very bad because if your image size is - f.e- 8MB, it'll take 8MB of RAM in your server, and probably, PHP request will timeout, because uploading, then saving to database, can take more than few seconds.
Do that if your images are of 1-2MB, and it could be a bad approach anyway. Binary files should be uploaded in chunks, in order to take as less as possible memory, since web pages should work in a shared hosting, but I don't know if it's your case or you're in a dedicated one, but anyway, again, you'll be wasting resources without uploading in chunks - and this is hard to achieve in MySQL for large files -.
At the end of the day, better store references to the file, or use a convention that let us image identifier (ID column in your database) to let your PHP script locate images in some folder.
Upvotes: 3
Reputation: 7094
FYI, it is a bad practice to store an image in database.
It is better to store an image in a folder, rather than mysql database. Refer image upload.
Just upload the image to a folder, and then save its relative path to the database.
Upvotes: 1
Reputation: 61771
You should not store the images inside your database. You should only store reference to location on disc.
Also this has been explained over here: Storing Images in DB - Yea or Nay?
Upvotes: 2
Reputation: 318488
I'll give you some keywords that might be useful for you:
move_uploaded_file()
(to store image data in the file system)$_FILES
(to access uploaded files in PHP)Upvotes: 2