| Server IP : 127.0.0.1 / Your IP : 216.73.216.48 Web Server : Apache/2.4.58 (Win64) OpenSSL/3.1.3 PHP/8.2.12 System : Windows NT DESKTOP-3H4FHQJ 10.0 build 19045 (Windows 10) AMD64 User : win 10 ( 0) PHP Version : 8.2.12 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : D:/xampp/htdocs-coblaa/pureFaith/ |
Upload File : |
<?php
// Set headers for JSON response and CORS
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
session_start();
$response = ['success' => false, 'message' => ''];
// Check for POST request method first
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$response['message'] = 'Invalid request method. Must be POST.';
echo json_encode($response);
exit;
}
// Check if user is authenticated
if (!isset($_SESSION['my_id']) || empty($_SESSION['my_id'])) {
$response['message'] = 'User not logged in.';
echo json_encode($response);
exit;
}
$user_id = $_SESSION['my_id'];
// Check for all required form data
if (!isset($_FILES['videoFile'], $_POST['videoName'], $_POST['thumbnailData'])) {
$response['message'] = 'Missing required data.';
echo json_encode($response);
exit;
}
// Database connection details
$servername = "localhost";
$username = "root";
$password = "2019khalaf";
$dbname = "purefaithdb";
// Create a new database connection
$con = new mysqli($servername, $username, $password, $dbname);
if ($con->connect_error) {
$response['message'] = "Connection failed: " . $con->connect_error;
echo json_encode($response);
exit;
}
// Sanitize and validate input faithName
$vid_name = trim($_POST['videoName']);
$faith_name = trim($_POST['faithName']);
$description = trim(isset($_POST['description']) ? $_POST['description'] : '');
// --- Video File Handling ---
$upload_dir = 'CB_video/';
if (!is_dir($upload_dir)) {
if (!mkdir($upload_dir, 0777, true)) {
$response['message'] = 'Failed to create video upload directory.';
echo json_encode($response);
exit;
}
}
$video_file_name = uniqid() . '-' . basename($_FILES['videoFile']['name']);
$temp_video_path = $_FILES['videoFile']['tmp_name'];
if (!move_uploaded_file($temp_video_path, $upload_dir . $video_file_name)) {
$response['message'] = 'Failed to move the uploaded video file.';
echo json_encode($response);
exit;
}
// --- Thumbnail Image Handling ---
$img = str_replace('data:image/jpeg;base64,', '', $_POST['thumbnailData']);
$img = str_replace(' ', '+', $img);
$thumb_img_binary = base64_decode($img);
$upload_thumb_dir = 'CB_vid_thumb/';
if (!is_dir($upload_thumb_dir)) {
if (!mkdir($upload_thumb_dir, 0777, true)) {
$response['message'] = 'Failed to create thumbnail upload directory.';
echo json_encode($response);
exit;
}
}
$thumb_file_name = uniqid() . '.jpg';
$thumb_path = $upload_thumb_dir . $thumb_file_name;
if (!file_put_contents($thumb_path, $thumb_img_binary)) {
$response['message'] = 'Failed to save the thumbnail image.';
echo json_encode($response);
exit;
}
// --- Database Insertion (using Prepared Statements) ---
$sql = "INSERT INTO cb_videotb (my_id, video_name, video_description, video_url, thumb_url, faith_id) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $con->prepare($sql);
if ($stmt === false) {
$response['message'] = 'Prepare failed: ' . $con->error;
} else {
// Bind parameters to match the SQL statement's columns
$get_video_faith ="select * from cb_faithtb where cb_faith='$faith_name'";
$run_get_video_faith = mysqli_query($con,$get_video_faith);
while($row_get_video_faith= mysqli_fetch_array($run_get_video_faith))
{
$faithId = $row_get_video_faith['id'];
$stmt->bind_param("ssssss", $user_id, $vid_name, $description, $video_file_name, $thumb_file_name, $faithId);
}
if ($stmt->execute()) {
$response['success'] = true;
$response['message'] = 'Video and thumbnail uploaded successfully!';
} else {
$response['message'] = 'Error inserting data: ' . $stmt->error;
}
// Close the statement
$stmt->close();
}
// Close the connection
$con->close();
echo json_encode($response);
?>