mardi 3 mars 2015

Sql Server image upload/download using PHP

I am having a problem that involves when I upload the image to a sql server database. Once uploaded then I try to retrieve the image and the file is 2 bytes smaller and will not display. I added some print statements that showed that the uploaded file before being placed into the database 14395 Bytes. When I retrieve it from the database it is only 14393 Bytes. The picture show ups on the webpage with the typical red x for file not found. Any thoughts? I have posted the PHP code and the table structure.


Notes: I have already checked the ini file for the max file size and added the ini set to the file to make sure the filesize is set to the max.



ini_set(mssql.textsize, 2147483647);
ini_set(mssql.textlimit, 2147483647);


Also the total size of the image is 14KB which is way below the 2MB limit.


Images Table:



filename varchar(100)
binaryfile varbinary(max)


PHP Code Upload image:



$fileHandle = fopen($_FILES["tmp_name"], "rb");
$tempContents = fread($fileHandle, filesize($_FILES["imagefile"]["tmp_name"]));
$fclose($fileHandle);

$fileContents = unpack("H*hex", $tempContents);
$contents = "0x".$fileContents["hex"];
mssql_query("INSERT INTO images(filename, binaryfile) VALUES ('".$_FILES["imagefile"]["name"]."',".$contents.")");


PHP Code to Retrieve the image:



$title=$_POST["imagetitle"];
$result=mssql_query("SELECT binaryfile FROM images WHERE filename='".$title."');
$row = mssql_fetch_assoc($result);
echo $row;

Aucun commentaire:

Enregistrer un commentaire