Database Reference
In-Depth Information
$_FILES["upload_file"]["size"]
$_FILES["upload_file"]["type"]
These variables represent the original filename on the client host, the temporary file‐
name on the server host, the file size in bytes, and the file MIME type. Be careful here
because there may be an entry for an upload field even if the user submitted no file. In
this case, the
tmp_name
value will be the empty string or the string
none
.
To simplify access to file upload information, use a utility routine that does the work.
The following function,
get_upload_info()
, takes an argument corresponding to the
name of a file upload field. Then it examines the
$_FILES
array and returns an associative
array of information about the file, or a
NULL
value if the information is not available.
For a successful call, the array element keys are
"tmp_name"
,
"name"
,
"size"
, and
"type"
:
function
get_upload_info
(
$name
)
{
# Check the $_FILES array tmp_name member to make sure there is a
# file. (The entry might be present even if no file was uploaded.)
$val
=
NULL
;
if
(
isset
(
$_FILES
[
$name
])
&&
$_FILES
[
$name
][
"tmp_name"
]
!=
""
&&
$_FILES
[
$name
][
"tmp_name"
]
!=
"none"
)
$val
=
$_FILES
[
$name
];
return
(
$val
);
}
See the
post_image.php
script for details about how to use this function to get image
information and store it in MySQL.
Uploads in Python
In Python, write a simple upload form like this:
print
(
'''
<form method="post" enctype="multipart/form-data" action="
%s
">
Image name:<br />
<input type="text" name="image_name", size="60" />
<br />
Image file:<br />
<input type="file" name="upload_file", size="60" />
<br /><br />
<input type="submit" name="choice" value="Submit" />
</form>
'''
%
(
os
.
environ
[
'SCRIPT_NAME'
]))
When the user submits the form, obtain its contents using the
FieldStorage()
method
of the
cgi
module (see
Recipe 20.5
). The resulting object contains an element for each
input parameter. For a file upload field, get this information as follows:
form
=
cgi
.
FieldStorage
()
if
form
.
has_key
(
'upload_file'
)
and
form
[
'upload_file'
]
.
filename
!=
''
: