PHP File Uploading

What is PHP File Uploading?

You can also upload files using PHP in an HTML form. At first the files are uploaded in initial/ temporary folder and later it is relocated to the target defined by PHP script. There are certain criteria for uploading files in PHP like upload_max_filesize. You can find information in the phpinfo.php about file uploads.

Example:
PHP Script (upload.php)
<?php
$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name)) {
    if (!empty($name)) {
        $location = 'upload/';
        $target_file = $location . $name;
        if (!file_exists($target_file)) {
            if (move_uploaded_file($tmp_name, $location . $name)) {
                echo('uploaded');
            }
        }else{
            echo 'File already exist.';
        }
    } else {
        echo 'please choose a file';
    }
}
?>

Note: Create upload folder.

PHP Script (index.php)
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>CP Tutorials</title>
</head>
<body>
<h3>File Upload:</h3>
<form action="upload.php" method="POST" enctype="multipart/form-data">
    Select file to upload: <input type="file" name="file"><br>
    <input type="submit" value="Submit">
</form>
</body>
</html>