Hey I'm kind of new to PHP, so any help is appreciated. I have decided to put images inside my database (I know many feel strongly against this). I am able to put an image in the database with the code below:
<?php
$handle = fopen("$_FILES['images/test.jpg']", "rb");
$img = fread($handle, filesize('$_FILES['images/test/jpg']'));
fclose($handle);
//die($img);
$img = base64_encode($img);
$con = mysql_connect('host','user','pass');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_name", $con); //Replace with your MySQL DB Name
$id=mysql_real_escape_string($_POST['id']); //This value has to be the same as in the HTML form file
$category=mysql_real_escape_string($_POST['category']); //This value has to be the same as in the HTML form file
$description=mysql_real_escape_string($_POST['description']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO interiors (id,category,description,image) VALUES ('$id','$category','$description','$img')"; /*form_data is the name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>
As you can see there are 4 fields. The first 3 are text and the last is the picture. Obviously when the picture is loaded its just a bunch of random characters. My problem is decoding the characters. The script below is what I use to query my results.
<html>
<head>
<title>Title</title>
</head>
<body>
<?php
$myvar = $_POST['formVar'];
$myvar2 = $_POST['formVar2'];
$myvar3 = $_POST['formVar3'];
$con = mysql_connect('host','user','pass');
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_name", $con); //Replace with your MySQL DB Name
if(isset($_POST["q"]))
$selection = $_POST["q"];
else
$selection = null;
switch ($selection)
{
case "all":
$query = "SELECT * FROM interiors";
break;
case "id":
$query = "SELECT * FROM interiors WHERE id like '%$myvar3%'";
break;
case "custom":
$query = $_POST["cq"];
break;
case "category":
$query = "SELECT * FROM interiors WHERE category='$myvar2'";
break;
case "search":
$query = "SELECT * FROM interiors WHERE description like '%$myvar%'";
break;
default:
echo 'Nothing to do here';
}
$q = mysql_query($query)
or die("This is not a valid query: " . $query);
$fieldCount = mysql_num_fields($q);
echo "You selected the query <i>$query</i>.<br />
Here are your results: <br /><br />
<table border='1'>
<tr>";
// print table headers
for($i=0; $i<$fieldCount; $i++)
{
$field = mysql_fetch_field($q);
echo "<th>{$field->name}</th>";
}
echo "</tr>\n";
// Print table rows
while($record = mysql_fetch_row($q))
{
echo "<tr>";
foreach($record as $cell)
echo "<td>$cell</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
As you can see the queries are pre-written and will not change. (besides the variables which were carried over from the form on the page before $myvar). How can I decode the picture? I know it has something to do with "$db_img = base64_decode($db_img);" but i cant quite figure out where it belongs and what else needs to be added where. (all images will be the same format- gif if that makes a difference). My tutorial came from http://mrarrowhead.com/php_tutorials_ma ... ql_php.php, but having to adjust it to my script is messing me up.
Help retrieving images from a database
Moderator: General Moderators
-
darren_plehn
- Forum Newbie
- Posts: 11
- Joined: Tue Oct 06, 2009 8:48 pm
Re: Help retrieving images from a database
Just a wild guess here, but do you know if you even encrypted these files when you decided to save them to the db?
If you didn't encrypt them, then there is nothing to decrypt.
If you did encrypt them with something like a hash (md5/sha2) there is no way to decrypt them.
if you used encrypt() or base64_encode(), then decrypt or base64_decode() is your answer.
If you didn't encrypt them, then there is nothing to decrypt.
If you did encrypt them with something like a hash (md5/sha2) there is no way to decrypt them.
if you used encrypt() or base64_encode(), then decrypt or base64_decode() is your answer.
-
darren_plehn
- Forum Newbie
- Posts: 11
- Joined: Tue Oct 06, 2009 8:48 pm
Re: Help retrieving images from a database
Thanks for the reply. I used "base64_encode()" to get the image in there. I think what it comes down to is that I'm having trouble correctly using "base64_decode()." I can't seem to implement it correctly so that it works. My query just keeps showing a bunch of random characters in the spot of the picture. (no decryption was used)
-
darren_plehn
- Forum Newbie
- Posts: 11
- Joined: Tue Oct 06, 2009 8:48 pm
Re: Help retrieving images from a database
I have taken another approach and no longer need input for this.