Picture File Upload Database PHP
I am trying to add a picture upload for a project that i am currently doing, but whenever i try this it does not display correctly on the page. But if i add the picture using phpMyadmin it displays fine on the webpage. To display the image on the page i am using this code.
For the upload i am working on, it involves a form, with other inputs beside the file upload, but the code below, is in its basic form
<img src="data:image/jpeg;base64,'. base64_encode( $row['review_picture'] ). '" style="height: 150px; width: 150px;" />
But the part which i am really struggling on is the page, which it is posted to.
<form action="new_review.php" method="post" name="myForm">
<label>Review Picture: </label><input type="file" name="review_picture" id="review_picture">
I have tried many different ways round this, what ever i try i can't quite get the hang of it, and tbf it the last piece of code is a bit of rough guess. I would be really grateful for any help.
$data = file_get_contents('$_POST[review_picture]');
$data = base64_encode($data);
mysqli_query($connection_Var,"INSERT INTO review(review_picture) VALUES ('$base64') or die (mysql_error());
header( 'Location: ../index.php');
$connection_Var = mysqli_connect("localhost","root","","review_db");
echo "Failed to connect to MySQL: " . mysqli_connect_error();
So what i'm thinking at the moment is that once the image has been posted to the php page, it then needs to be converted to the base64, which i think is correct, because i use base64 to display a image uploaded directly to the database. And then the base64 code, should then be added to to "INSERT INTO" etc, right? But i just don't actually know how i would actually do this.
My first suggestion would be to not store the actual image in the DB, since it's just a binary file that you are never going to search or sort on. Instead, store in the file system (since it is, in fact, just a file), perhaps generating some sort of unique file name for it (see uniqid(), etc.), then store information about the image (including its file name) in the database. Then your image-serving script just needs to query the DB to get the applicable file name, send an appropriate content-type header() (probably also retrieved from the DB), then do a readfile() on the actual image file.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
How to Ask Questions the Smart Way
(not affiliated with this site, but well worth reading)
Nice topic, its really helpful for me, thanks to all of you for sharing your ideas and feedback.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread