connect_error){ die("Connection failed: " . $conn->connect_error); } // API endpoints if "action" parameter is provided if (isset($_GET['action'])) { $action = $_GET['action']; if ($action === 'getPlaylist') { header("Content-Type: application/json"); $sql = "SELECT * FROM songs ORDER BY uploaded_at DESC"; $result = $conn->query($sql); $songs = array(); while ($row = $result->fetch_assoc()) { $songs[] = $row; } echo json_encode($songs); $conn->close(); exit; } if ($action === 'uploadSong') { header("Content-Type: application/json"); // Process the song file upload if (isset($_FILES['song'])) { $uploadDir = 'uploads/'; if (!is_dir($uploadDir)) { mkdir($uploadDir, 0777, true); } $songName = basename($_FILES['song']['name']); $targetSong = $uploadDir . $songName; $songType = strtolower(pathinfo($targetSong, PATHINFO_EXTENSION)); // Allow only MP3 files if ($songType !== "mp3") { echo json_encode(array("error" => "Only MP3 files are allowed for the song.")); exit; } if (move_uploaded_file($_FILES['song']['tmp_name'], $targetSong)) { $songURL = $targetSong; } else { echo json_encode(array("error" => "Error uploading the song file.")); exit; } } else { echo json_encode(array("error" => "No song file received.")); exit; } // Process optional cover image upload $coverURL = ''; if (isset($_FILES['cover']) && $_FILES['cover']['error'] == UPLOAD_ERR_OK) { $coverName = basename($_FILES['cover']['name']); $targetCover = $uploadDir . $coverName; $coverType = strtolower(pathinfo($targetCover, PATHINFO_EXTENSION)); // Allow only specific image types $allowed = array("jpg", "jpeg", "png", "gif"); if (!in_array($coverType, $allowed)) { echo json_encode(array("error" => "Cover image must be jpg, jpeg, png, or gif.")); exit; } if (move_uploaded_file($_FILES['cover']['tmp_name'], $targetCover)) { $coverURL = $targetCover; } } // Get additional data from the POST request $title = $conn->real_escape_string($_POST['title'] ?? $songName); $artist = $conn->real_escape_string($_POST['artist'] ?? 'Uploaded Artist'); $lyrics = $conn->real_escape_string($_POST['lyrics'] ?? ''); $stmt = $conn->prepare("INSERT INTO songs (title, file, cover, artist, lyrics) VALUES (?, ?, ?, ?, ?)"); $stmt->bind_param("sssss", $title, $songURL, $coverURL, $artist, $lyrics); if ($stmt->execute()){ echo json_encode(array( "success" => "File uploaded successfully", "song" => array( "id" => $stmt->insert_id, "title" => $title, "file" => $songURL, "cover" => $coverURL, "artist" => $artist, "lyrics" => $lyrics ) )); } else { echo json_encode(array("error" => "Database error: " . $conn->error)); } $stmt->close(); $conn->close(); exit; } } ?>
Your playlist is empty
Unknown artist