/    Sign up×
Community /Pin to ProfileBookmark

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.

[code=html]<img src=”data:image/jpeg;base64,’. base64_encode( $row[‘review_picture’] ). ‘” style=”height: 150px; width: 150px;” />[/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

[code=html]<form action=”new_review.php” method=”post” name=”myForm”>
<label>Review Picture: </label><input type=”file” name=”review_picture” id=”review_picture”>
</form>[/code]

But the part which i am really struggling on is the page, which it is posted to.

[code=php]<?php
Open_Connection();

global $connection_Var;
$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’);
Close_Connection();

function Open_Connection()

{
global $connection_Var;
$connection_Var = mysqli_connect(“localhost”,”root”,””,”review_db”);
if (mysqli_connect_errno($connection_Var))
{
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}
}
function Close_Connection()
{
global $connection_Var;
mysqli_close($connection_Var);
}
?>[/code]

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.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@dav991authorJun 08.2013 — 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.
Copy linkTweet thisAlerts:
@NogDogJun 08.2013 — 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 [i]about[/i] 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 D?, then do a readfile() on the actual image file.
Copy linkTweet thisAlerts:
@noahwilsonJun 13.2013 — Nice topic, its really helpful for me, thanks to all of you for sharing your ideas and feedback.
×

Success!

Help @dav991 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...