I am doing some freelance work with PHP and I am posting these for my future reference. This is basic PHP 101 stuff here.The HTML Form:
<form enctype="multipart/form-data" action="upload.php" method="post">
Please upload your file:
<div>
<input id="fileUpload" name="fileUpload" type="file">
<input id="submit" name="submit" value="Submit" type="submit">
</div>
</form>The key part to the form is enctype="multipart/form-data" which tells the server to expect a file on postback.
The upload php file to process the file:
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['fileUpload']['name']);
if (isset($_FILES['fileUpload']))
{
if(move_uploaded_file($_FILES['fileUpload']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['fileUpload']['name']).
" has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
}
?>First we test to see if fileUpload has a value if so it attempts to save the file to the target path. If all goes well we get a message that the upload passed. If not a message that there was a problem.