🐘
upload_audio.php
Back
📝 Php ⚡ Executable Ctrl+S: Save • Ctrl+R: Run • Ctrl+F: Find
<?php header("Access-Control-Allow-Origin: *"); header("Content-Type: application/json"); $uploadDir = __DIR__ . "/audio/"; // Create directory if it doesn't exist if (!file_exists($uploadDir)) { mkdir($uploadDir, 0755, true); } if (!isset($_FILES['audio'])) { echo json_encode(['success' => false, 'error' => 'No file uploaded']); exit; } $file = $_FILES['audio']; $allowedTypes = ['audio/mpeg', 'audio/mp3']; if (!in_array($file['type'], $allowedTypes)) { echo json_encode(['success' => false, 'error' => 'Invalid file type. Only MP3 allowed.']); exit; } // Generate unique filename $extension = pathinfo($file['name'], PATHINFO_EXTENSION); $filename = uniqid('audio_') . '.' . $extension; $targetPath = $uploadDir . $filename; if (move_uploaded_file($file['tmp_name'], $targetPath)) { $url = 'audio/' . $filename; echo json_encode(['success' => true, 'url' => $url]); } else { echo json_encode(['success' => false, 'error' => 'Failed to save file']); } ?>