Image insert problem php sqlserver
- by Termedi
Hi
I can not save image inside a sql server 2000 database.
datatype is image
here is the code:
// Image Upload
<?php
include('config.php');
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$size = filesize($tmpName);
set_magic_quotes_runtime(0);//to desactive the default escape spacials caracters made by PHP in the externes files
$img_binaire = base64_encode(fread(fopen(str_replace("'","''",$tmpName), "r"), $size));
$query = "INSERT INTO test_image (image_name, image_content, image_size) ".
"VALUES ('{$fileName}','{$img_binaire}', '{$size}')";
odbc_exec($conn, $query) or die('Error, query failed');
echo "<br>File $fileName uploaded<br>";
echo "<br>File Size: $fileSize <br>";
}
?>
// Image Show
<?php
include('config.php');
$sql = "select * from test_image where id =2";
$rsl = odbc_exec($conn, $sql);
$image_info = odbc_fetch_array($rsl);
//$count = sizeof($image_info['image_content']);
//header('Accept-Ranges: bytes');
//header('Content-Length: '.$image_info['image_size']);
//header("Content-length: 17397");
header('Content-Type: image/jpeg');
echo base64_decode($image_info['image_content']);
//echo bindec($image_info['image_content']);
?>
Error: Warning: odbc_exec()
[function.odbc-exec]: SQL error:
[Microsoft][ODBC SQL Server
Driver][SQL Server]Operand type clash:
text is incompatible with image, SQL
state 22005 in SQLExecDirect in
C:\xampp\htdocs\test\upload.php on
line 25 Error, query failed