using PHP to store image in MSSQL database

images php coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Tue Oct 12, 2010 9:54 pm

using PHP to store image in MSSQL database

Postby sucithra » Tue Oct 12, 2010 10:01 pm

i am using sql server 2008:

database stucture is:

CREATE TABLE [dbo].[fun_gallery](
[file_id] [int] NOT NULL,
[file_name] [varchar](50) NOT NULL,
[file_type] [varchar](50) NOT NULL,
[file_size] [int] NOT NULL,
[file_content] [image] NOT NULL,

$fileName = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];

$fp = fopen($tmpName, 'rb');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);

i call the upload function:

upload function:
function file_upload($max_id,$filename,$filesize,$filetype,$filecontent)
//$file='CAST('.$filecontent.' AS '.'VARBINARY(MAX))';
$safesql = new SafeSQL_ANSI;
$continue = 1;

$sql_ins_login = "INSERT INTO fun_gallery("
."file_id,file_name, file_type,file_size,file_content"
.") VALUES ("
."%i,'%s', '%s', %i, '%s'"
$sql_ins_login_val = array($max_id,$filename,$filetype,$filesize,$filecontent);
$safe_ins_login = $safesql->query($sql_ins_login, $sql_ins_login_val); // echo $safe_ins_login; echo "<br/>"; exit;
$qry_ins_login_val = $this->db_link->query($safe_ins_login);
if(!$qry_ins_login_val) {
$continue = 0;

if($continue) {
return 1;
} else {
return 0;

and this is the download function:

function download_file($file_id)
$safesql = new SafeSQL_ANSI;

$sql_select_file = "SELECT file_name,file_type,file_type,file_size,file_content FROM fun_gallery where file_id=$file_id";
$qry_select_file = $this->db_link->query($sql_select_file);
if ($qry_select_file) {
$total_found = $this->db_link->num_rows;

if ($total_found > 0) {
$row_get_file = $this->db_link->fetch_row($qry_select_file);
return $row_get_file;

i called up the download function and the stored values frm the database:

$file_name = $file_download['file_name'];
$file_size = $file_download['file_size'];
$file_type = $file_download['file_type'];
$file_content = $file_download['file_content'];

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-type: ".$file_type);
header("Content-Length: ".$file_size);
header("Content-Disposition: attachment; filename=\"".$file_name."\";");
header("Content-Transfer-Encoding: binary");

$html ='';
$html ='<html>
/*$html ='<html>
echo $html;

when i download the file , its open with
something '/0' is there in full file.
for image its show some error and for pdf file also..

thanks in advance..

Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest