2

My question is how to send a file to a field of type image from Laravel to SQL Server?

$image  = file_get_contents($file);
$sql    = "INSERT INTO tabla ( CONTENIDO  ) VALUES (?);";
$params = array(array($image, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY)));

This way I can only insert varbinary and what I need to be inserted into image type.

Zhorov
  • 28,486
  • 6
  • 27
  • 52
  • As per the question guide, please do not post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. – Dale K Nov 30 '21 at 21:30
  • And what actually is your question? You've shown us a code dump... what isn't working as desired? – Dale K Nov 30 '21 at 21:31
  • 1
    the question is how to send a file to a field of type image from laravel to sql server ?? – Carlos Rodriguez Nov 30 '21 at 21:36
  • You need to show what research you have done, what you have tried and where you got stuck. Otherwise the question it too board for this site. – Dale K Nov 30 '21 at 21:40
  • 2
    You should be using `varbinary(max)` instead of `image` - the latter data type has been deprecated since at least SQL Server 2005... _[IMPORTANT!](https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql?view=sql-server-ver15) ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead._ – AlwaysLearning Nov 30 '21 at 22:17
  • I understand, but that database is already being used by a system and I am making an api, the client is asking me to insert it in image – Carlos Rodriguez Nov 30 '21 at 22:27
  • You may try to pass `SQLSRV_SQLTYPE_IMAGE` in the `sqlsrv_query` call: `$params = array(array($image, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_IMAGE));`. A similar example can be found [here](https://stackoverflow.com/questions/53660167/inserting-a-file-in-to-the-sql-database-and-retrieving-it/53664747#53664747). – Zhorov Dec 01 '21 at 07:15

0 Answers0