File Upload

PHP is able to receive files as part of a form submission.

PHP is capable of receiving file uploads from any RFC-1867, RFC-2854 compliant browser. It also supports multipart/form-data format, from the RFC-2388.

File upload code is based on the $_FILES superglobal, and move_uploaded_file() function. It also relies on several PHP directives: file_uploads, upload_max_filesize, upload_tmp_dir, post_max_size and max_input_time.

<?php

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

    echo '<pre>';
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo 'File is valid, and was successfully uploaded.';
    } else {
        echo 'Possible file upload attack!';
    }

?>

Documentation

See also Learn Everything About File Upload in PHP With Examples, Secure File Uploads in PHP, How to Upload Files in PHP (Step-by-Step Guide for Beginners), RFC-1867, RFC-2854 and RFC-2388.

Related : $_FILES