We help IT Professionals succeed at work.

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

jhines2000 asked
Last Modified: 2013-11-05
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!
// Connect to Database
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 );
    $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">
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
*****************************<end index.php>
*****************************<begin images.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>";
		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>
// Connect to Database
$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 );
*****************************<end retrieve_binary_data.php>

Open in new window

Watch Question

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.
Top Expert 2007
This one is on us!
(Get your first solution completely free - no credit card required)


@_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 :)
Top Expert 2007

Did you also remove the image_destroy-line?


@hernst42- yes, it is commented out as well..
Top Expert 2007

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.
<img src="23592298.php">
header('Content-Type: image/x-icon');
echo file_get_contents('23592298.ico');

Open in new window


im going to check it out asap!



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



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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.