troubleshooting Question

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

Avatar of jhines2000
jhines2000 asked on
PHPMySQL Server
9 Comments1 Solution2847 ViewsLast Modified:
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>
icon.zip
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros