Uploading files is something that is commonly needed on a website for contact requests, quote requests, and also for special members only sections of websites where drawings, documents... need to be uploaded. Using php to upload files is pretty easy using the move_uploaded_file function.
First you have a form that will collect the documents that will be uploaded, such as:
<form action="testupload2.php" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>
Next you need to create a file that will actually grab the files and move them from your local computer to the web server. The code below will take in 1 or more files and upload them to the root directory. If you would like to upload more than one file you would just create another <input type="file" name="pictures[]"> field.
<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "$name");
}
}
?>
The example above will upload files to the directory that this file is in. If you wanted to upload to a different directory then you would just change the second parameter of the move_uploaded_file function. For example if I wanted to upload to the folder /rootdirectory/myuploads then the move_uploaded_file() function would look like this:
move_uploaded_file($tmp_name, "/rootdirectory/myuploads/$name");