- Lid geworden
- 13 aug 2013
- Berichten
- 5.509
Hiermee kan je verder. Er zit veel controle in.
Het is de aangepast www.php.net/manual/en/features.file-upload.php . Daar vind je meer info.
Het is de aangepast www.php.net/manual/en/features.file-upload.php . Daar vind je meer info.
PHP:
<?php
// Geef het path, bijvoorbeeld "/map" of als "/map/submap"
$upload_path = $_SERVER["DOCUMENT_ROOT"] . "/uploads/afbeeldingen";
// max. file grootte in bytes
$max_filesize = 5000 * 1024;
// toegestane file extensies
$file_ext = array (
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
);
// Benodigde variabele
$msg = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
try {
// Controleer op 'Corruptie Attack'
if (!isset($_FILES['upfile']['error']) || is_array($_FILES['upfile']['error'])) {
throw new RuntimeException("Onjuiste bestand parameters");
}
// Controleer $_FILES errors
switch ($_FILES['upfile']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException("Geen bestand gevonden.");
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException("Bestand groter dan limiet.");
default:
throw new RuntimeException("Onbekende fout opgetreden.");
}
// Controleer file grootte
if ($_FILES['upfile']['size'] > $max_filesize) {
throw new RuntimeException("Bestand is te groot.");
}
// Controleer MIME Type
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search (
$finfo->file($_FILES['upfile']['tmp_name']), $file_ext, true
)) {
throw new RuntimeException("Onjuiste extensie.");
}
// Plaats file met unieke naam in map $path
// Verander de volgende regel als je de echte naam wilt gebruiken
$move_filename = sha1_file($_FILES['upfile']['tmp_name']);
if (!move_uploaded_file( $_FILES['upfile']['tmp_name'],
sprintf($upload_path . '/%s.%s', $move_filename, $ext)
)) {
throw new RuntimeException("Bestand kan niet verplaatst worden.");
}
// Hier is de upload gelukt
$msg = "Bestand is met succes geupload.";
// Hier is de upload niet gelukt
} catch (RuntimeException $e) {
$msg = $e->getMessage();
}
}
// Toon bericht
if ($msg != "") {
$file_parts = explode(".", $_FILES['upfile']['name']);
echo '<div id="message">';
echo 'Het bestand <b>' . $_FILES['upfile']['name'] . '</b> is van type <b>' . $_FILES['upfile']['type'] . "</b>";
echo '<br>Opgeslagen met bestandsnaam <b>' . $move_filename . "." . $file_parts[count($file_parts) - 1] . "</b>";
echo '<br>De grootte is <b>' . $_FILES['upfile']['size'] . '</b> bytes.';
echo '</div>';
}
?>
<div style="width:100%; max-width:350px; margin:15px auto; padding:15px; border:1px solid #ccc;">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<br><b>Maximaal:</b> <?php echo round($max_filesize / 1024); ?> kb
<br><b>Extensies:</b>
<?php
$ext_array = array_keys($file_ext);
for ($i = 0; $i < count($file_ext); $i++ ) {
echo $ext_array[$i] . " ";
}
?>
<br><br><input type="file" name="upfile" size="40">
<br><br><input type="submit" value="Upload">
</form>
</div>