Jump to content

[SOLVED] image upload to mssql


Shazbot!

Recommended Posts

I have been pondering the web, these forums and other materials trying to upload an image to a MS SQL2005 database.

The database field that will hold the image is datatype: image (BLOB).

 

So far I am able to upload the image (I think) into the database but I am not sure if this is correct or not because when I try to view it I get an image error message:

 

here is the upload script [validation code purposely left out for testing]

//code taken from: http://www.php-mysql-tutorial.com/php-mysql-upload.php
require_once('Connect/conn.php');
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

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

//I have also tried
//$content = addslashes(fread(fopen($_FILES['userfile']['tmp_name'], "rb"), $_FILES['userfile']['size']));


$SQL="INSERT INTO LEAD_Uploaded_Files(FILE_NAME, FILE_Type, FILE_Size, FILE_PID, FILE_Content) VALUES 
	('$fileName', '$fileType', '$fileSize', 8231954, '$content')";

mssql_query($SQL) or die('Error, query failed');

 

 

 

here is the code for viewing:

$SQL="SELECT File_Content, File_Type FROM Uploaded_Files WHERE FILE_PID=8231954";
$SQL_Query=mssql_query($SQL);


$result = mssql_query($SQL);
$row = mssql_fetch_array($result);

header(Content-type: $row['File_Type']);
echo $row['File_Content'];

 

 

 

 

Link to comment
Share on other sites

You need quotes around the header() value.

 

header("Content-type: $row[File_Type]");

 

If you want to know if an image is being put into the db, you might be able to do something like:

 

SELECT LEFT(40,File_Content),LENGTH(File_Content) FROM Uploaded_Files;

 

...to see how much was inserted and if the image id is what you expect.  (That query would work in MySQL, but I'm not familiar with MSSQL to know what functions to use.  The idea is to check just the beginning of the binary string and the size.)

Link to comment
Share on other sites

unfortunately that did not work and the query to test it out on the SQL Server is

SELECT LEFT(40,File_Content),LEN(File_Content) FROM LEAD_Uploaded_Files;

This gives me the following error:

Operand type clash: image is incompatible with int

 

So I am assuming that the uploaded image is not being converted to a binary file.

 

Also, the view image script is displaying this message:

GIF89a and some strange characters which cannot be posted here.

 

I am hoping some else looking at this might have another idea?

Thanks in advance!

Link to comment
Share on other sites

The "GIF89a...." is the file header for a GIF image, which suggests the image data is making it into the database.  That's what I was hoping LEFT(40,File_Content) would return.

 

Have you double-checked the header information and mimetype?  If you're getting binary data as text from the view script, it often means that the header did not properly identify the mimetype to the browser.

Link to comment
Share on other sites

here is the code to display the  image:

require_once('Connect/conn.php');
$SQL="SELECT File_Content, File_Type FROM Uploaded_Files WHERE FILE_PID=8231954";
$SQL_Query=mssql_query($SQL);

$result = mssql_query($SQL);
$row = mssql_fetch_array($result);

header('Content-type: image/gif');
//header('Content-type: $row[1]'); I have also tried this
echo $row[0];

 

There is only 1 record in the database that is a .gif file.

In FireFox I get an outline of the image and the following error:

"image cannot be display because it contains errors."

 

The source file shows the garbled text field "GIF89a ...."

 

 

In IE it just shows an image place holder.

 

Link to comment
Share on other sites

Okay, so the image content is probably getting inserted incorrectly.

 

Try inserting it again.  Look at this post and change some of the insertion script.

 

<?php
// Also change this in the output script:

header("Content-type: $row[1]");  // You need double quotes for interpolation

?>

Link to comment
Share on other sites

Wildbug,

Thanks for all your help, I have been working on this problem for quite some time now and it has been a pain. The article that you posted fixed the issue. I have successfully uploaded a .jpeg and .gif and downloaded it for viewing. I would never have figured it out without your help. Thanks a lot!

 

here is the solution that work for me:

 

Code to upload:

require_once('Connect/conn.php');

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

$datastring= file_get_contents($tmpName);
$data=unpack("H*hex", $datastring);

$SQL="INSERT INTO Uploaded_Files(FILE_NAME, FILE_Type, FILE_Size, FILE_PID, FILE_Content) VALUES 
	('$fileName', '$fileType', '$fileSize', 8231954, 0x".$data['hex'].");";

echo $SQL;

mssql_query($SQL) or die('Error, query failed');

 

 

code to view:

require_once('Connect/conn.php');
$SQL="SELECT File_Content, File_Type FROM Uploaded_Files WHERE FILE_PID=8231954";
$SQL_Query=mssql_query($SQL);

$result = mssql_query($SQL);
$row = mssql_fetch_array($result);

//$row[1]
header("Content-type: $row[1]");
echo $row[0];

 

 

All I need to do now is validation and I should be set to go. Thanks again!

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.