Brinkster Knowledge Base

KB Home   /  Support  /  Code Snippets  /   PHP Upload Form - Windows
PHP Upload Form - Windows Articles
This code sample contains a PHP Upload Script sample that can be used to upload any file to a specified folder within your website. It is important that you change the login password for the application.

This code sample is will function for Paid Windows Hosting accounts.

Please copy and paste the following code as directed.

Save the following code as upload.php
Create the directory named "uploads" at the same directory as your upload.php file.

Change the password from 'secret' to a password of your choice.
Please note this is a minimalistic way of securing your upload form.

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

function print_form ()
{
    echo "<p><h1>Enter file to upload</h1></p>";
    echo "<form method='post' enctype='multipart/form-data'>";
    echo "<input type='file' size='40' name='file1'><br>";
    echo "Password: <input type='password' name='passwd'><br>";
    echo "<input type=submit value='Upload!'>";
    echo "</form>";
}
if (!$_FILES)
{
    echo "<html><head><title>File Uploader</title></head>";
    echo "<body>";
    print_form ();
}
//----  Change the password from 'secret' to 'somethingelse' here ----:                                                        
elseif ($_POST['passwd'] != 'secret')                                                
{
    echo "<h1>Password Incorrect</h1>";
    print_form ();
}
else{
$uploaddir = getcwd () . '\\uploads\\';
$uploadfile = $uploaddir . basename($_FILES['file1']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['file1']['tmp_name'], $uploadfile)) {
    echo "<h2>File is valid, and was successfully uploaded.</h2>";
} else {
    echo "File Upload Unsuccessful.";
}
}
?>