Upload Files using PHP - File Upload Script

Leave a Comment

First, we need to create the HTML. HTML forms that include file upload fields must include an ENCTYPE argument:
ENCTYPE="multipart/form-data"

File Upload Script

<html>
<head>
<title>Listing 9.15 A file upload script</title>
</head>
<?php
$file_dir = "/home/matt/htdocs/uploads";
$file_url =
"http://80-www.corrosive.co.uk.proxy.lib.uiowa.edu/matt/uploads";
if ( isset( $fupload ) )
{
print "path: $fupload<br>\n";
print "name: $fupload_name<br>\n";
print "size: $fupload_size bytes<br>\n";
print "type: $fupload_type<p>\n\n";
if ( $fupload_type == "image/gif" )
{
copy ( $fupload, "$file_dir/$fupload_name") or die ("Couldn't copy");
print "<img src=\"$file_url/$fupload_name\"><p>\n\n";
}
}
?>
<body>
<form enctype="multipart/form-data" action="<?php print $PHP_SELF?>"
method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="51200">
<input type="file" name="fupload"><br>x<input type="submit" value="Send file!">
</form>
</body>
</html>

File Upload Global Variables


  • $fupload - Path to temporary variable
  • $fuploadname - Name of uploaded file
  • $fuploadsize - Size (in bytes) of uploaded file
  • $fupload type - MIME type of
  • uploaded file (where given by client)


0 comments:

Post a Comment