Yazılım Geliştirme
1 dakika okuma
PHP Güvenli Dosya Yükleme
PHP ile guvenli dosya yukleme: MIME dogrulama, boyut siniri ve depolama.
E
Emre Cihan Guvenlik Kontrolleri
class DosyaYukleme {
private const MAX = 10 * 1024 * 1024;
private const MIME = ['image/jpeg','image/png','image/webp','application/pdf'];
private const EXT = ['jpg','jpeg','png','webp','pdf'];
public function yukle(array $f): string {
if ($f['error'] !== UPLOAD_ERR_OK) throw new Exception('Yukleme hatasi');
if ($f['size'] > self::MAX) throw new Exception('Dosya cok buyuk');
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($f['tmp_name']);
if (!in_array($mime, self::MIME, true)) throw new Exception('Gecersiz tur');
$ext = strtolower(pathinfo($f['name'], PATHINFO_EXTENSION));
if (!in_array($ext, self::EXT, true)) throw new Exception('Gecersiz uzanti');
$isim = bin2hex(random_bytes(16)) . '.' . $ext;
move_uploaded_file($f['tmp_name'], UPLOAD_PATH . '/' . $isim);
return $isim;
}
}
Upload Klasoru .htaccess
<FilesMatch ".php$">
Require all denied
</FilesMatch>
Options -ExecCGI -Indexes
Guvenli dosya yukleme cok katmanli dogrulama gerektirir.
Paylaş:
Twitter / X
E
Emre Cihan
Sistem Yöneticisi · Maker · Elektronik Meraklısı
İlgili yazılar