Example 1 — Save an Uploaded Photo
Handle a form field named photo and store the file in the uploads folder.
<?php
if (isset($_FILES['photo']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) {
$tmp = $_FILES['photo']['tmp_name'];
$dest = __DIR__ . '/uploads/' . basename($_FILES['photo']['name']);
if (move_uploaded_file($tmp, $dest)) {
echo "Upload saved as " . basename($dest), PHP_EOL;
}
} How It Works
- The script first checks that a file arrived and that there was no upload error.
- tmp_name is the temporary path where PHP stored the upload.
- basename() removes any folder parts from the user's file name for safety.
- move_uploaded_file() moves the file and returns true when it works.

