Simple script for file uploads
<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
# ---------------------------------------------------------------------------
# $_POST Validation
# ---------------------------------------------------------------------------
if ( ! $_POST) {
throw new RuntimeException('Form not submitted');
}
if ( ! $_FILES) {
throw new RuntimeException('File not submitted');
}
# ---------------------------------------------------------------------------
# $_FILES Validation - Procedural
# ---------------------------------------------------------------------------
try {
if ($_FILES['upload']['error'] !== 0) {
throw new Exception('error field missing');
}
$uploadDirectory = 'uploads';
$fileInfo = new SplFileInfo($_FILES['upload']['name']);
$filenameHash = hash('md5', basename($_FILES['upload']['name']));
$uploadFilename = $uploadDirectory . DIRECTORY_SEPARATOR . $filenameHash . '.' . $fileInfo->getExtension();
if (move_uploaded_file($_FILES['upload']['tmp_name'], $uploadFilename)) {
echo 'Successfully uploaded file';
}
} catch (RuntimeException $e) {
echo $e->getMessage();
}
<!DOCTYPE html>
<html>
<body>
<form method="post" action="uploadFile.php" enctype="multipart/form-data">
Select image to upload:
<input type="hidden" name="MAX_FILE_SIZE" value="10000" />
<input type="file" name="upload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>