2

I'm trying to figure out a way to store files in a database. I know it's recommended to store files on the file system rather than the database, but the job I'm working on would highly prefer using the database to store these images (files).

There are also some constraints. I'm not an admin user, and I have to make stored procedures to execute all the commands. This hasn't been of much difficulty so far, but I cannot for the life of me establish a way to store a file (image) in the database.

When I try to use the BULK command, I get an error saying "You do not have permission to use the bulk load statement." The bulk utility seemed like the easy way to upload files to the database, but without permissions I have to figure a work-a-round.

I decided to use an HTML form with a file upload input type and handle it with PHP. The PHP calls the stored procedure and passes in the contents of the file. The problem is that now it's saying that the max length of a parameter can only be 128 characters.

Now I'm completely stuck. I don't have permissions to use the bulk command and it appears that the max length of a parameter that I can pass to the SP is 128 characters.

I expected to run into problems because binary characters and ascii characters don't mix well together, but I'm at a dead end...

Thanks

3
  • question asked several times on SO already... Commented Mar 16, 2010 at 15:25
  • I've been stuck with this problem for a couple days now and I've looked around for answers and even had asked another similar question. Commented Mar 16, 2010 at 16:09
  • @MitchWheat no it has not Commented May 20 at 19:53

2 Answers 2

0

Here is an example I've found in David Hayden's blog.

It's a c# example, but the steps should be similar in PHP:

  1. Convert your uploaded file to a byte array
  2. Execute dynamic TSQL on the server
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the example. I'm going to a meeting to figure out this whole mess, as right now I don't think I have the correct permissions to get it working.
This answer should have had more detail - the linked article has gone now.
The link is not valid anymore
link is dead. downvoted
-1

In general, we don't pass binary data in SQL. We upload the file to the server, then load the image from the server into the database.

Load the image into the database from a file:

UPDATE images
SET image = LOAD_FILE('images/myimage.jpg')
WHERE image_id = 1234

Get the image back out to a file:

SELECT image
INTO DUMPFILE 'images/myimage.jpg'
FROM images
WHERE image_id = 1234

1 Comment

does not answer the question. op did not ask if it was practice, he asked how to do it

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.