Last active
September 3, 2021 05:19
-
-
Save BlackScorp/4b68e124743c31ac1f97271dcd56aac3 to your computer and use it in GitHub Desktop.
Code zum Video https://youtu.be/bo_e_ESldJg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="POST" action="upload.php" enctype="multipart/form-data"> | |
<input type="file" name="datei[]"> | |
<button type="submit">Upload</button> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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