<?php
// Yüklemelerin kaydedileceği klasör
$targetDir = __DIR__ . "/uploads/";
// Klasör yoksa oluştur
if (!is_dir($targetDir)) {
mkdir($targetDir, 0755, true);
}
// uploads içine PHP çalışmasını engelle (.htaccess oluştur)
$htaccess = $targetDir . ".htaccess";
if (!file_exists($htaccess)) {
file_put_contents($htaccess, "php_flag engine off");
}
// Dosya bilgileri
$file = $_FILES["fileToUpload"] ?? null;
if (!$file || $file["error"] === UPLOAD_ERR_NO_FILE) {
die("Lütfen bir dosya seçin.");
}
// Maksimum 5MB
$maxSize = 5 * 1024 * 1024;
if ($file["size"] > $maxSize) {
die("Dosya çok büyük! Maksimum boyut: 5MB");
}
// Dosya uzantısı
$originalName = basename($file["name"]);
$sanitizedName = preg_replace("/[^A-Za-z0-9_\-\.]/", "_", $originalName); // temiz ad
$fileExtension = strtolower(pathinfo($sanitizedName, PATHINFO_EXTENSION));
// İzin verilen uzantılar
$allowedExtensions = ["jpg", "jpeg", "png", "gif", "pdf", "txt"];
if (!in_array($fileExtension, $allowedExtensions)) {
die("Bu dosya uzantısına izin verilmiyor!");
}
// MIME türü doğrulama (gerçek dosya formatını kontrol eder)
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->file($file["tmp_name"]);
$allowedMime = [
"jpg" => "image/jpeg",
"jpeg" => "image/jpeg",
"png" => "image/png",
"gif" => "image/gif",
"pdf" => "application/pdf",
"txt" => "text/plain"
];
if (!isset($allowedMime[$fileExtension]) || $mimeType !== $allowedMime[$fileExtension]) {
die("Dosya türü geçersiz veya sahte!");
}
// Benzersiz isim ile kaydet
$newFileName = uniqid("upload_", true) . "." . $fileExtension;
$targetFile = $targetDir . $newFileName;
// Dosyayı taşı
if (move_uploaded_file($file["tmp_name"], $targetFile)) {
echo "Dosya başarıyla yüklendi!<br>";
echo "Orijinal dosya adı: " . htmlspecialchars($originalName) . "<br>";
echo "Sunucuya kaydedilen ad: " . htmlspecialchars($newFileName) . "<br>";
if (in_array($fileExtension, ["jpg", "jpeg", "png", "gif"])) {
echo '<img src="uploads/' . htmlspecialchars($newFileName) . '" style="max-width:300px; margin-top:10px;">';
}
} else {
echo "Dosya yüklenirken bir hata oluştu.";
}
?>