Processing File Uploads
Problem
You want to allow files to be uploaded to your web server and stored in your database.
Solution
Present the user with a web form that includes a file field. When the user submits the form, extract the file and store it.
Discussion
One special kind of web input is an uploaded file. A file is
sent as part of a post
request, but
it’s handled differently from other post
parameters, because a file is
represented by several pieces of information such as its contents, its
MIME type, its original filename on the client, and its name in
temporary storage on the web server host.
To handle file uploads, you must send a special kind of form to the user; this is true no matter what API you use to create the form. When the user submits the form, the operations that check for and process an uploaded file are API-specific.
To create a form that enables files to be uploaded, the opening
<form>
tag should specify the
post
method and must also include
an enctype
(encoding type)
attribute with a value of multipart/form-data
:
<form method="post" enctype="multipart/form-data" action="script_name
">
If the form is submitted using the application/x-www-form-urlencoded
encoding
type, file uploads will not work properly.
To include a file upload field in the form, use an <input>
element of type file
. For example, to present a 60-character
file field named upload_file
, the
element looks like this:
<input type="file" name="upload_file" size="60" />
The browser displays this field as a text ...
Get MySQL Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.