سيدعلي بوفىكاس
';
var_dump($_FILES['image']);
echo '';
// Check for upload errors
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
// Provide specific error messages based on the error code
switch ($_FILES['image']['error']) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$error_message = "خطأ: حجم الملف أكبر من الحد المسموح به في إعدادات الخادم.";
break;
case UPLOAD_ERR_PARTIAL:
$error_message = "خطأ: لم يتم رفع الملف بالكامل.";
break;
case UPLOAD_ERR_NO_FILE:
$error_message = "ملاحظة: لم يتم اختيار أي ملف. سيتم استخدام الصورة الافتراضية.";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$error_message = "خطأ في الخادم: مجلد الملفات المؤقتة مفقود.";
break;
case UPLOAD_ERR_CANT_WRITE:
$error_message = "خطأ في الخادم: فشل في كتابة الملف على القرص.";
break;
case UPLOAD_ERR_EXTENSION:
$error_message = "خطأ في الخادم: تم إيقاف رفع الملف بواسطة إضافة PHP.";
break;
default:
$error_message = "خطأ غير معروف أثناء رفع الملف. الرمز: " . $_FILES['image']['error'];
}
} else {
// No upload error, proceed with validation
$allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
$max_size = 5 * 1024 * 1024; // 5MB
$detected_type = $_FILES['image']['type']; // Using fallback method
if (!in_array($detected_type, $allowed_types)) {
$error_message = "خطأ: نوع الملف غير مسموح به. المسموح به: JPEG, PNG, GIF, WebP.";
} elseif ($_FILES['image']['size'] > $max_size) {
$error_message = "خطأ: حجم الملف أكبر من 5 ميجابايت.";
} else {
// *** CHANGE: Corrected the upload directory path ***
$upload_dir = 'uploads/articles/'; // Removed '../'
// Create directory if it doesn't exist
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
// Check if directory is writable
if (is_writable($upload_dir)) {
$file_extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$file_name = 'article_' . time() . '_' . uniqid() . '.' . $file_extension;
$upload_path = $upload_dir . $file_name;
if (move_uploaded_file($_FILES['image']['tmp_name'], $upload_path)) {
$image_path = 'uploads/articles/' . $file_name; // Success!
} else {
$error_message = "خطأ: فشل في نقل الصورة من المجلد المؤقت إلى المجلد النهائي. قد يكون هناك مشكلة في صلاحيات الخادم.";
}
} else {
$error_message = "خطأ: مجلد الصور غير قابل للكتابة. المسار: " . $upload_dir;
}
}
}
}
// 3. If no errors, proceed to database insertion
if (empty($error_message)) {
$db_host = 'localhost';
$db_user = 'hopita08_aqalam_siha';
$db_pass = 'Sidali1987@';
$db_name = 'hopita08_aqalam_siha';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("فشل الاتصال بقاعدة البيانات: " . $conn->connect_error);
}
$conn->set_charset("utf8mb4");
$stmt = $conn->prepare("INSERT INTO articles (title, content, category_id, author_id, status, image, created_at) VALUES (?, ?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("ssisss", $title, $content, $category_id, $author_id, $status, $image_path);
if ($stmt->execute()) {
header("Location: add_article.php?status=success");
exit();
} else {
$error_message = "فشل حفظ المقال في قاعدة البيانات: " . $stmt->error;
}
$stmt->close();
$conn->close();
}
}
// --- PAGE DISPLAY ---
if (isset($_GET['status']) && $_GET['status'] == 'success') {
$success_message = "تم إضافة المقال بنجاح!";
}
$db_host = 'localhost';
$db_user = 'hopita08_aqalam_siha';
$db_pass = 'Sidali1987@';
$db_name = 'hopita08_aqalam_siha';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
$conn->set_charset("utf8mb4");
$categories_result = $conn->query("SELECT * FROM categories ORDER BY name ASC");
$authors_result = $conn->query("SELECT id, fullname FROM users WHERE role = 'author' OR role = 'admin' ORDER BY fullname ASC");
$conn->close();
?>
إضافة مقال جديد - أقلام الصحة
العودة للقائمة