D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
skyconb
/
newsyacine
/
Filename :
add_article.php
back
Copy
<?php // =============================================== // add_article.php (إضافة مقال جديد مع تحميل صورة) // =============================================== session_start(); include 'db_connect.php'; // التحقق من الصلاحية $is_admin = ($_SESSION['admin_logged_in'] ?? false) === true && ($_SESSION['admin'] ?? 'user') === 'admin'; if (!$is_admin) { $_SESSION['error'] = "ليس لديك الصلاحية لإضافة مقالات."; header("Location: admin.php?section=articles"); exit(); } $error = ''; $title = $_POST['title'] ?? ''; $content = $_POST['content'] ?? ''; $slug = $_POST['slug'] ?? ''; $category = $_POST['category'] ?? 'General'; $thumbnail_path = ''; // سيخزن مسار الصورة المرفوعة if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_article'])) { $title = trim($_POST['title']); $content = $_POST['content']; // يتم إنشاء العنوان المختصر (slug) تلقائياً إذا لم يقم المستخدم بإدخاله $slug = strtolower(trim(str_replace(' ', '-', $_POST['slug'] ?: $title))); $category = $_POST['category']; $author_id = $_SESSION['admin_user_id'] ?? 1; // =============================================== // 🎯 منطق تحميل الصورة (Handling File Upload) // =============================================== if (isset($_FILES['thumbnail_file']) && $_FILES['thumbnail_file']['error'] === UPLOAD_ERR_OK) { $file_tmp_name = $_FILES['thumbnail_file']['tmp_name']; $file_name = $_FILES['thumbnail_file']['name']; $file_extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); // توليد اسم ملف فريد ومنع التداخل $unique_name = uniqid() . '.' . $file_extension; $upload_dir = 'uploads/'; $destination = $upload_dir . $unique_name; // التحقق من نوع الملف $allowed_types = ['jpg', 'jpeg', 'png', 'gif']; if (!in_array($file_extension, $allowed_types)) { $error = "الرجاء رفع ملف صورة صالح (JPG, PNG, GIF)."; } elseif (!is_dir($upload_dir) && !mkdir($upload_dir, 0777, true)) { $error = "فشل إنشاء مجلد الرفع: $upload_dir"; } else { if (move_uploaded_file($file_tmp_name, $destination)) { $thumbnail_path = $destination; // هذا هو المسار الذي سيخزن في DB } else { $error = "حدث خطأ أثناء تحميل الملف. تأكد من صلاحيات الكتابة للمجلد 'uploads/'."; } } } else { // يمكن ترك الصورة اختيارية أو إضافة خطأ إذا كانت إجبارية // $error = "الرجاء رفع صورة مصغرة للمقال."; } // =============================================== if (empty($title) || empty($content) || empty($slug)) { $error = "الرجاء ملء جميع الحقول المطلوبة (العنوان والمحتوى والعنوان المختصر)."; } if (!$error) { // التحقق من تكرار العنوان المختصر (Slug) $check_sql = "SELECT id FROM articles WHERE slug = ?"; $check_stmt = $conn->prepare($check_sql); $check_stmt->bind_param("s", $slug); $check_stmt->execute(); if ($check_stmt->get_result()->num_rows > 0) { $error = "العنوان المختصر (Slug) مستخدم بالفعل. اختر اسماً فريداً."; } $check_stmt->close(); } if (!$error) { $sql = "INSERT INTO articles (title, content, slug, category, thumbnail_url, author_id) VALUES (?, ?, ?, ?, ?, ?)"; $stmt = $conn->prepare($sql); if ($stmt) { // نستخدم $thumbnail_path الآن $stmt->bind_param("sssssi", $title, $content, $slug, $category, $thumbnail_path, $author_id); if ($stmt->execute()) { $_SESSION['message'] = "تم إضافة المقال بنجاح!"; header("Location: admin.php?section=articles"); exit(); } else { $error = "خطأ في الإضافة: " . $stmt->error; } $stmt->close(); } } } ?> <!DOCTYPE html> <html lang="ar" dir="rtl"> <head> <meta charset="UTF-8"> <title>إضافة مقال جديد</title> <style> body { font-family: Tahoma, sans-serif; margin: 0; background-color: #f0f2f5; display: flex; justify-content: center; align-items: center; min-height: 100vh; } .form-container { background: white; padding: 40px; border-radius: 8px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); width: 90%; max-width: 800px; } h2 { color: #2c3e50; border-bottom: 2px solid #eee; padding-bottom: 10px; margin-bottom: 25px; text-align: center; } .form-group { margin-bottom: 20px; } label { display: block; margin-bottom: 8px; font-weight: bold; color: #333; } input[type="text"], input[type="url"], input[type="file"], textarea { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; font-size: 14px; } textarea { resize: vertical; } .status-message { padding: 10px; margin-bottom: 15px; border-radius: 4px; text-align: center; font-weight: bold; } .error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; } .submit-btn { width: 100%; padding: 12px; background-color: #f1c40f; color: #333; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; font-weight: bold; transition: background-color 0.3s; } .submit-btn:hover { background-color: #e6b10d; } .back-link { display: block; margin-top: 25px; text-align: center; color: #3498db; text-decoration: none; font-weight: bold; } </style> </head> <body> <div class="form-container"> <h2>إضافة مقال جديد</h2> <?php if ($error): ?><div class="status-message error"><?php echo $error; ?></div><?php endif; ?> <form method="POST" enctype="multipart/form-data"> <div class="form-group"> <label for="title">عنوان المقال:</label> <input type="text" name="title" required value="<?php echo htmlspecialchars($title); ?>"> </div> <div class="form-group"> <label for="slug">العنوان المختصر (Slug - للرابط):</label> <input type="text" name="slug" placeholder="اتركه فارغاً ليتم إنشاؤه تلقائياً من العنوان" value="<?php echo htmlspecialchars($slug); ?>"> </div> <div class="form-group"> <label for="thumbnail_file">تحميل صورة مصغرة (JPG, PNG, GIF):</label> <input type="file" name="thumbnail_file" id="thumbnail_file" accept="image/*"> </div> <div class="form-group"> <label for="category">التصنيف:</label> <input type="text" name="category" value="<?php echo htmlspecialchars($category); ?>"> </div> <div class="form-group"> <label for="content">محتوى المقال:</label> <textarea name="content" rows="15" required><?php echo htmlspecialchars($content); ?></textarea> </div> <button type="submit" name="add_article" class="submit-btn">نشر المقال</button> </form> <a href="admin.php?section=articles" class="back-link">العودة لإدارة المقالات</a> </div> </body> </html> <?php $conn->close(); ?>