1

I want to save (insert) a uploaded file to a database with PHP, which the type of the database filed is varbinary.
Finally I want to have the content of VarBinary (output) like when file is read in C# and then is stored in byte array and array is inserted to VarBinary.
Also my connection to the database is with sqlsrv.
The type of my files are just PDF and images.
I try this code but my output is different with the output of C#:

$handle=@fopen($_FILES["my_file"]["tmp_name"], 'rb');
$content= file_get_contents($_FILES["my_file"]["tmp_name"]);
$content = unpack("N*",$content);
$content=  implode($content);
$sql = "INSERT INTO files (file_data) VALUES (CONVERT(varbinary(MAX)?)";
$params=array();
array_push($params,$content);
$table=sqlsrv_query( $conn, $sql, $params);

"$conn" is the name of my connection that works correctly.

5
  • 3
    Why is this tagged c#? Commented Nov 2, 2016 at 13:20
  • @Botonomous because I want output like the output of C#. Commented Nov 2, 2016 at 13:25
  • I think what he is going for is PHP# ? Commented Nov 2, 2016 at 13:25
  • @yasaman-ghassemi What do you mean 'output like the output of C#' Do you mean console output? Commented Nov 2, 2016 at 13:33
  • @Botonomous no, my mean is the content that is stored in database.The content of that varbinary field. Commented Nov 2, 2016 at 13:35

2 Answers 2

3

PHP doesn't have a "byte array" data type. What it has is a string type, which is a byte array for all intents and purposes. To read the binary content of a file into a variable which is as close to a byte array as you'll ever get in PHP, do:

$content = file_get_contents($_FILES['my_file']['tmp_name']);

Yup, that's it. Nothing more to do.

I'm not particular familiar with the sqlsrv API, but perusing its documentation it appears that you can (need to?) set a flag this way to flag the data as being binary:

sqlsrv_query($conn, 'INSERT INTO files (file_data) VALUES (?)', array(
    array($content, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING, SQLSRV_SQLTYPE_BINARY)
));
Sign up to request clarification or add additional context in comments.

Comments

1

I propose to always convert your binary data to base64. So it can be stored in database easily and also it can be transfered from somewhere to anywhere with minimum headache!

$handle=@fopen($_FILES["my_file"]["tmp_name"], 'rb');
$elephantContent = file_get_contents($_FILES["my_file"]["tmp_name"]);
$rabbitContent = base64_encode($elephantContent);
//Now ...
$sql = "INSERT INTO files (file_data) VALUES (?)";
sqlsrv_query($conn , $sql , array($rabbitContent) );

file_data field in files table can be varchar, varbinary, blob, text ! :)

Now it can be invoked from database and be packed into a img tag directly.

<img src="data:image/jpeg;base64,PUT `file_data` CONTENTS HERE!" alt="..." />

You can store file type and alt properties in database and put them in tag.

You even can convert it to binary in database (if you insist on seeing binary data in db) (mysql 5.6+)

SELECT FROM_BASE64(`file_data`) as elephant_content from `files` WHERE ...

... And I'm pretty sure that there is equivalent method to do it so in SQL. For example read this: https://social.technet.microsoft.com/wiki/contents/articles/36388.transact-sql-convert-varbinary-to-base64-string-and-vice-versa.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.