homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

PHP file upload to MySql field
Adding file to specific record

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

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!



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

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 ;))



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

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.


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

You need to specify which record (clearly ;))

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



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

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!


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

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



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

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?


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

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]


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

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.


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

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.


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

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?


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

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?


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

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved