hey i have form which has a file field. After sel开发者_JAVA技巧ecting a file,i want to insert the selected file into the database column. how do i do it?
I wouldn't recommend using the database for files content because the database is limited when it comes to this. Sure there are text
fields that you can put the content into, but even those are limited to a certain degree. In any case, the text
field type is being deprecated in the future. I would get into the habit of trying to stay away from it, unless you really need to have it. You can just as easily open up the content when needed, using fopen
, fgets
, fclose
. Or fread
, or file_get_contents
instead of storing it into the database, why not store it as a file on the server, using file_put_contents
.
However, just noticing that maybe you are referring to the file name being stored in the database, if so than that would work great and use a varchar(255) should be perfectly fine.
In this case, make sure to have a method="post" and an action="whatever.php" for the form set. And ENCTYPE = "multipart/form-data"
. Than a normal submit button will trigger the action and you can handle it directly from there. You can use move_uploaded_file
to store it within a path on the server, or if it's a text file and you need to do some editing to it, one of the methods I mentioned above should do it. Otherwise, to get the file from a form, use the $_FILES
predefined PHP Array. You'll want to get the $_FILES['file_input_name']['tmp_name']
and $FILES['file_input_name']['name']
values, where file_input_name
= the name attribute for the tag for your file input within the form.
Just 4 steps:
- You have to setup the form correctly: add ENCTYPE = "multipart/form-data" in the form tag.
- In your php page you have to read the content of the file
- Escape the binary data using mysql_real_escape_string()
- Using a standard query put the file contents into you row.
精彩评论