June 30, 2004

File uploads in PHP

Writing a PHP page and need to accept files from a form? Here's the skinny, according to the PHP Manual entry on Handling file uploads:

First, you will of course need the HTML form:

<form enctype="multipart/form-data" action="_URL_" 
                   method="post"> 
   <input type="hidden" name="MAX_FILE_SIZE" 
                   value="30000" /> 
   Send this file: <input name="userfile" type="file" /> 
   <input type="submit" value="Send File" /> 
</form>

Then you'll need to save the file in the PHP page:

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES 
              //should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . $_FILES['userfile']['name'];

print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], 
                           $uploadfile)) {
   print "File is valid, and was successfully uploaded. ";
   print "Here's some more debugging info:\n";
   print_r($_FILES);
} else {
   print "Possible file upload attack!  Here's some ";
   print "debugging info:\n";
   print_r($_FILES);
}
print "</pre>";

?>
Technorati tags: PHP | file uploads | HTML forms | html | uploading files | uploading |

src="http://feeds.feedburner.com/~s/ChaosMagnet?i=http://www.chaosmagnet.com/blog/archives/000454.html" type="text/javascript" charset="utf-8">
Posted by roadnick at June 30, 2004 11:47 AM | TrackBack

Comments

Post a comment









Remember personal info?