<?php
include('header.php');
if (!isset($_FILES['pdf']) || !isset($_FILES['thumbnail'])) {
echo "error: الملفات غير موجودة";
exit;
}
$pdfFile = $_FILES['pdf'];
$thumbFile = $_FILES['thumbnail'];
$allowedPdfExt = ['pdf'];
$allowedImgExt = ['jpg','jpeg','png'];
$pdfExt = strtolower(pathinfo($pdfFile['name'], PATHINFO_EXTENSION));
$imgExt = strtolower(pathinfo($thumbFile['name'], PATHINFO_EXTENSION));
// تحقق من الامتدادات
if (!in_array($pdfExt, $allowedPdfExt)) {
echo "error: نوع ملف PDF غير مدعوم";
exit;
}
if (!in_array($imgExt, $allowedImgExt)) {
echo "error: نوع ملف الصورة غير مدعوم";
exit;
}
// مسار حفظ الملفات
$uploadDir = __DIR__ . "/../service/images/pdf/";
if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);
// حماية أسماء الملفات (لتفادي الثغرات الأمنية)
function sanitizeFileName($filename) {
// إزالة أي حرف غير مسموح به في اسم الملف
return preg_replace('/[^a-zA-Z0-9-_\.]/', '_', $filename);
}
$pdfName = sanitizeFileName(basename($pdfFile['name']));
$imgName = sanitizeFileName(basename($thumbFile['name']));
$pdfPath = $uploadDir . $pdfName;
$imgPath = $uploadDir . $imgName;
if (file_exists($pdfPath) || file_exists($imgPath)) {
echo "error: الملف موجود مسبقاً ولا يمكن رفعه مرتين";
exit;
}
// رفع ملفات PDF و الصورة
if (!move_uploaded_file($pdfFile['tmp_name'], $pdfPath)) {
echo "error: فشل رفع ملف PDF";
exit;
}
if (!move_uploaded_file($thumbFile['tmp_name'], $imgPath)) {
echo "error: فشل رفع ملف الصورة";
exit;
}
echo "تم رفع الملفات بنجاح";
exit;