Link to home
Start Free TrialLog in
Avatar of jhines2000
jhines2000

asked on

store / retrieve / display ICON files into MySQL database using PHP

Hello All Experts!

I am building a database of ICONS, literally ICO files and would like to display them in a webpage using PHP, MySQL.

I have been able to upload, insert and retrieve / display all images types: JPG, PNG, GIF, etc... however, for the life of me, I cannot get an icon file to display.

I have attached my form, index.php, which allows uploading of images, all types, into MySQL.  That part seems to work fine for all images as I get data going into the database.

I have attached images.php, a small PHP script to loop through and output all images in a simple html table and retrieve_binary_data.php which handles the query/retrieve of the images from the database.

I have attached an icon.zip (icon.ico.zip, please remove .ZIP from the extracted file.  EE doesnt allow ICO files!?) , which is the sample icon I am working with.

Again, all the normal image types display ok, just icons fail :(

I hope this is enough information, Thanks experts!
*****************************<index.php>
<?php
 
//---------------------
// Connect to Database
//---------------------
include('db_config.php');
 
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
 
$handle      = fopen($tmpName, 'r');
$img = fread($handle , filesize($tmpName));
fclose($handle );
 
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
 
$img = base64_encode($img); 
 
$sql = "insert into ids_blob values(null,'$fileType','$img','$fileSize', Null, '$fileName');"; 
 
mysql_query($sql) or die('SQL Insert Failed!!!'.mysql_error()); 
 
echo "Success! You have inserted your picture!";
}
?> 
 
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
*****************************<end index.php>
 
*****************************<begin images.php>
<?php 
 
include('db_config.php');
 
$query = "SELECT count(*) as num FROM ids_blob";
 
$total_pages = mysql_fetch_array(mysql_query($query));
$nrows = $total_pages[num];
//echo "numrows-".$nrows;
 
echo "<table border='1'><tr align='center' valign='top'>";
for ($i = 1; $i <= $nrows; $i++ ) {
 
	$sql = "select image_id, image_name from ids_blob where image_id ='$i'"; 
	$result = mysql_query($sql) or die('SQL Query Failed!!!'.mysql_error()); 
	$row = mysql_fetch_array($result);
 
	$id = $row['image_id'];
	$name = $row['image_name'];
         //start new row every 10th picture/icon, using PHP modulus to check for perfect division by 10....
	if ($i == (($i-1) % 10 == 0)){
		echo "<tr align='center' valign='top'><td  width ='60'height= '60'>";
		echo $id."<br><img src=\"retrieve_binary_data.php?id=".$id."\">";
		echo "</td>";
	}else{
		echo "<td  width ='60'height= '60'>";
		echo $id."<br><img src=\"retrieve_binary_data.php?id=".$id."\">";
		echo "</td>";
	}
}
echo "</tr></table>";
 
?>
<img src="icon.ico"> <!--- just to prove that an ICON display using a standard html image tag --->
*****************************<end images.php>
 
*****************************<begin retrieve_binary_data.php>
<?php
 
//---------------------
// Connect to Database
//---------------------
 
include('db_config.php');
 
$id = $_GET['id'];
$sql = "select image, image_type from ids_blob where image_id ='$id'"; 
 
$result = mysql_query($sql) or die('Bad query at 12!'.mysql_error()); 
 
while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
 
	$db_img = $row['image'];
	$type = $row['image_type'];
 
	$db_img = base64_decode($db_img); //print_r($db_img );
	echo($db_img);
}
imagedestroy($db_img);
?> 
*****************************<end retrieve_binary_data.php>

Open in new window

icon.zip
Avatar of _Nopik_
_Nopik_

Can you tell which part is faulty? E.g. does icon store properly to database? Please check if it does, then you will split the problem into half, either storage or display is wrong.
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jhines2000

ASKER

@_Nopik- the displaying of the icon file from the database.  I certain it is storing ok, I have data stored in the table in the column.  Data type is a blob value.

@hernst42- I tried all the content types / mime, they seem to not make a difference.  I tried image/vnd.microsoft.icon, image/x-icon etc....

I am at a loss, it shouldn't be this hard :)
Did you also remove the image_destroy-line?
@hernst42- yes, it is commented out as well..
Hm works perfect with this little example and your ico-file.

Either your data stored in the database is corrupt or there is another problem.
View the Source of the broken image to see php errors/warnings.
<html>
<body>
<img src="23592298.php">
</body>
</html>
 
<?php
header('Content-Type: image/x-icon');
echo file_get_contents('23592298.ico');

Open in new window

im going to check it out asap!

thanks!
This solution did work!  I copied and pasted a character in there and didn't see it initially.

thanks!
Sorry hernst42, your original solution was accurate, I mis-copied the example into my code.  :)