开发者

Displaying Images from SQL Database

I've recently been working on a static content handler project, and this is hitting me hard. I cannot get the images to show up in the browser the same way I could before. The image is correct, and it's impossible because my code pretty much stayed the same.

ob_start();

function ImageExistsSql($img)
{
 $rows = mssql_num_rows(mssql_query("SELECT id FROM Static_Image WHERE name = N'{$img}'"));
 if($rows > 0)
 {
  return true;
 }
 else
 {
  return false;
 }

}

function ImageExistsFile($img)
{
 if(!file_exists("images/".$img))
 {
  return false;
 }
 else
 {
  return true;
 }
}

function CreateImageFile($img)
{
 if(!ImageExistsFile($img))
 {
  $b =& new hex2bin;
  $data = mssql_fetch_row(mssql_query("SELECT data FROM Static_Image WHERE name = N'{$img}'"));
  $file = fopen("images/".$img, 'a+');
  fwrite($file, $b->convert($data));
  fclose($file);
 }

}

function GetExtension($img)
{
 return mssql_fetch_row(mssql_query("SELECT extension FROM Static_Image WHERE name = N'{$img}'"));
}

function WriteImageDataToScreen($img)
{
 $r = mssql_fetch_row(mssql_query("SELECT data FROM Static_Image WHERE name = N'{$img}'"));
 $d = str_split($r);

 for($i = 0; $i < strlen($r); $i++)
 {
  echo($d[$i]);
 }
}

function ContentHeader($img)
{
 $ext = GetExtension($img);
 switc开发者_如何学编程h($ext[0])
 {
  case "png":
  case ".png":
   header("Content-type: image/png");
   break;

  case "gif":
  case ".gif":
   header("Content-type: image/gif");
   break;

  case "jpg":
  case "jpeg":
  case ".jpg":
  case ".jpeg":
   header("Content-type: image/jpeg");
   break;

  default:
   die("[STATIC HANDLER] Extension: {$ext} not found");
   break;

 }
}

if(isset($_GET['i']))
{
 $imgf = $_GET['i'];
 $ext = GetExtension($imgf);

 if(ImageExistsSql($imgf))
 {
  if(ImageExistsFile($imgf))
  {
   ContentHeader($imgf);
   //$fn = fopen("images/".$imgf, 'rb');
   //fpassthru($fn);
                    // fpassthru isn't working anymore, used to work.
   WriteImageDataToScreen($imgf);
  }
  else
  {
   CreateImageFile($imgf);
   header("Location: {$_SERVER['REQUEST_URI']}");
  }
 }
 else
 {
  die("[STATIC HANDLER] IMAGE NON EXISTANT");
 }
}
else
{
 die("[STATIC HANDLER] FAILED TO PROCESS FILE");
} 

ob_end_flush();

The table Static_Image is as follows

CREATE TABLE [dbo].[Static_Image](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [name] [varchar](50) NOT NULL,
 [extension] [varchar](6) NULL,
 [dateadded] [date] NULL,
 [data] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

the data column is used, however, wasn't being used until recently as a way to expand this onto multiple servers.


in

$data = mssql_fetch_row(mssql_query("SELECT data FROM Static_Image WHERE name = N'{$img}'"));
$file = fopen("images/".$img, 'a+');
fwrite($file, $b->convert($data));

should that be $b->convert($data[0]) ?

note, it's often easier to store the image on disk exclusively, and just keep the pathname in the db...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜