Welcome to WebmasterWorld Guest from 54.160.131.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP file upload to MySql field

Adding file to specific record

     

cyclic

4:17 pm on Sep 15, 2002 (gmt 0)

10+ Year Member



I'm trying to upload a file to a specific record which I have selected from a MySql table ie. add additional info to just one field of a record. How do I ensure that the upload goes into the correct record? I have been using the url to select the record but can't quite figure how to upload to that record. At the moment the upload just ends up as a new record but in the correct field. I feel the solution is nearby!

Nick_W

4:33 pm on Sep 15, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Are you really trying to 'upload' a file, or just some text/etc to a record?

Check out the UPDATE command. (I think that's right ;))

Nick

cyclic

8:37 pm on Sep 15, 2002 (gmt 0)

10+ Year Member



Yep, I am trying to "upload" an image to a BLOB field. I can upload the image , but not to the correct record at the moment.

I shall check out "upload", thanks.

Nick_W

8:46 pm on Sep 15, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You need to specify which record (clearly ;))

Why not post the code, it's a bit tricky to guess what's going on....

Nick

cyclic

8:54 pm on Sep 15, 2002 (gmt 0)

10+ Year Member



Bit difficult to post the code at the moment as I am at work and switching between two machines! I have used a url variable from one page to the next to select the correct record but I can't figure how to upload a file to that record using "file" as the attribute for a form. I think I will post the code tomorrow if you can still help!

Nick_W

9:01 pm on Sep 15, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yeah, I've got email notification on this thread.. no worries.

Nick

cyclic

10:18 am on Sep 16, 2002 (gmt 0)

10+ Year Member



Sucess at last! I have now managed to get the upload into the correct record and into the blob column but it is just showing text ie. the path to the file instead of the actual file.

Do I have to escape some of the characters or is it a case of actually writing the file to the database. I have looked for some scripts to give me some hints and one has the following:

$data = addslashes(fread(fopen($form_data, "rb"), filesize($form_data)))

Not sure what the "b" is for in the file mode?

andreasfriedrich

10:33 am on Sep 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure what the "b" is for in the file mode?

binary - useful only on systems which differentiate between binary and text files such as Windows

fopen [php.net]

andreasfriedrich

10:40 am on Sep 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but it is just showing text ie. the path to the file instead of the actual file

$_FILES just contains infos about the uploaded file. $_FILES['userfile']['tmp_name'] will give you the name of the temporary file on the server. To store the file to the database you have to open it and read it as suggested by the little code snippet you included in your post.

cyclic

11:03 am on Sep 16, 2002 (gmt 0)

10+ Year Member



Thank you very much. I couldn' find the "b" in either of my php books. The fread/fopen is the clue. I shall have another go. By the way, is there a problem with storing binary info in MySql? This is for a customer to upload his images and saves me having to instruct him how to upload to a separate directory.

jatar_k

5:28 pm on Sep 16, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Why don't you just store the images in a designated directory and then store the path in the DB? Is it absolutely necessary to store the whole image in the DB?

cyclic

8:10 pm on Sep 16, 2002 (gmt 0)

10+ Year Member



The only reason for storing in the database was to allow the customer an easy way to update. I suppose I could set up a file upload page and then put the path in the database along with the rest of the details. In a form, is there a means to hide the path and then add the file to it perhaps by a variable? It just means that there has to be two operations for each change. I don't know if it wold be a good idea to allow a user access to the directories for deletion etc?

jatar_k

8:45 pm on Sep 16, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You can have the script do everything and they wouldn't see anything exactly the same as if it was dumping into the DB. I was just figuring it would be easier on the DB. I hate putting images into a db unless they are tiny. I prefer to have a dir for them and just build the path with the script and put that into the db.

You can just hard code the path into the script. Have a designated dir for the uploaded images or something.

>>user access to the directories

The scipt would have the access, not the user.