Skip to content

Instantly share code, notes, and snippets.

@BlackScorp
Last active September 3, 2021 05:19
Show Gist options
  • Save BlackScorp/4b68e124743c31ac1f97271dcd56aac3 to your computer and use it in GitHub Desktop.
Save BlackScorp/4b68e124743c31ac1f97271dcd56aac3 to your computer and use it in GitHub Desktop.
<form method="POST" action="upload.php" enctype="multipart/form-data">
<input type="file" name="datei[]">
<button type="submit">Upload</button>
</form>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$isPost = strtolower($_SERVER['REQUEST_METHOD']) === 'post';
$hasFiles = isset($_FILES['datei']['tmp_name'][0]) && (bool)$_FILES['datei']['tmp_name'][0];
if (!$isPost || !$hasFiles) {
header('Location: index.php');
}
$dateiPfad = $_FILES['datei']['tmp_name'][0];
$fileSize = filesize($dateiPfad);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, $dateiPfad);
if ($fileSize === 0) {
echo "Datei ist leer";
die();
}
$erlaubteTypen = [
'image/png' => 'png',
'image/jpg' => 'jpg'
];
if (!in_array($type, array_keys($erlaubteTypen))) {
echo "Ungültiger Dateityp";
die();
}
$erweiterung = $erlaubteTypen[$type];
$zielPfad = __DIR__ . '/files/' . time() . '.' . $erweiterung;
if (!copy($dateiPfad, $zielPfad)) {
echo "Konnte " . $dateiPfad . " nicht nach " . $zielPfad . " kopieren";
die();
}
unlink($dateiPfad);
echo $zielPfad . " erfoglreich hochgeladen";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment