Load .jpg from database

I want to make a simple link collection in flash , with thumbs of the pages http://robert.dap.ro/links
I figured out how to made all what I want , but sometimes the thumb is generated but not loaded. I used blob(), for images in the database and with a small script I generate the image:



<?php
require_once("config/config.inc.php");

if (isset($_GET['category'])) {
    $category = $_GET['category'];

}
if (isset($_GET['id'])) {
    $id = $_GET['id'];
} 
    
    $query = "select thumb from $category where id=$id";
    $result = mysql_query($query);
    $data = mysql_result($result,0,"thumb");
    
$filename = 'thumb.jpg';
if (is_writable($filename)) {
    if (!$handle = fopen($filename, 'w')) {
         print ("Cannot open file ($filename)");
         exit;
    }
    if (fwrite($handle, $data) === FALSE) {
        print("Cannot write to file ($filename)");
        exit;
    }
   print ("result=ok");
    fclose($handle);
    

} else {
    print("The file $filename is not writable");
}
?> 



so far everything working fine.

after the image is generated I load back in flash


var thumb = new LoadVars();
    thumb.sendAndLoad("generate_thumb.php?category="+category+"&id="+id, thumb, "POST");
    thumb.onLoad = function(success) {
        
        delete description_mc.procent_holder.onEnterFrame;
        description_mc.procent_holder._alpha = 100;
        //description_mc.thumb_holder.loadMovie("thumb.jpg");
        main_preload();
        my_mc.loadClip("thumb.jpg?"+category+"_"+id, "description_mc.thumb_holder");
    };

sometimes it works perfect , sometime not.

Do you any ideea what should I change ???