D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home5
/
rakcha
/
public_html
/
Filename :
test.php
back
Copy
<?php session_start(); // ✅ Session check must come AFTER session_start if (!isset($_SESSION['admin_name'])) { header("location: index.php"); exit; } // ✅ Database connection $host = "localhost"; $user = "rakcha_db"; $pass = "rakcha_db"; $db = "rakcha_db"; $con = new mysqli($host, $user, $pass, $db); if ($con->connect_error) { die("Connection failed: " . $con->connect_error); } // ✅ Handle form submission $message = ""; if (isset($_POST['add']) && $_POST['add'] === 'Upload') { $type = $_POST['file_type']; // image or video $desc = $_POST['description']; // text input $file = $_FILES['media_file']; // Choose upload directory $targetDir = ($type == "image") ? "galleryImage/" : "galleryVideo/"; $fileName = basename($file['name']); $targetFilePath = $targetDir . $fileName; // Upload file if (move_uploaded_file($file['tmp_name'], $targetFilePath)) { // Save to DB $stmt = $con->prepare("INSERT INTO gallery2 (filename, type, description) VALUES (?, ?, ?)"); $stmt->bind_param("sss", $fileName, $type, $desc); if ($stmt->execute()) { $message = "✅ File uploaded and saved to database successfully!"; } else { $message = "❌ Failed to insert data into the database."; } $stmt->close(); } else { $message = "❌ File upload failed."; } } ?> <!DOCTYPE html> <html> <head> <title>Upload Image or Video</title> </head> <body> <h2>Upload Image or Video</h2> <?php if (!empty($message)): ?> <p style="color: green;"><?php echo $message; ?></p> <?php endif; ?> <form method="POST" enctype="multipart/form-data"> <label>Select File Type:</label><br> <input type="radio" name="file_type" value="image" required> Image <input type="radio" name="file_type" value="video" required> Video <br><br> <label>Select File:</label><br> <input type="file" name="media_file" accept="image/*,video/*" required><br><br> <label>Description:</label><br> <input type="text" name="description" placeholder="Enter description" required><br><br> <input type="submit" value="Upload" name="add"> </form> </body> </html>