![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/private_html/utils/ |
<?php
// Download and store audio files locally
require_once 'config/database.php';
echo "<h1>🎵 Download and Store Audio Files</h1>";
$pdo = getDBConnection();
if (!$pdo) {
echo "<p style='color: red;'>❌ Database connection failed!</p>";
exit;
}
// Create audio directory if it doesn't exist
$audioDir = 'audio_files';
if (!is_dir($audioDir)) {
mkdir($audioDir, 0755, true);
echo "<p style='color: green;'>✅ Created audio directory: $audioDir</p>";
}
// Find tracks with external API BOX URLs
$stmt = $pdo->prepare("SELECT * FROM music_tracks WHERE audio_url LIKE '%api.box%' AND status = 'complete' ORDER BY created_at DESC");
$stmt->execute();
$tracks = $stmt->fetchAll();
if (empty($tracks)) {
echo "<p style='color: orange;'>⚠️ No tracks found with external API BOX URLs</p>";
} else {
echo "<p style='color: green;'>✅ Found " . count($tracks) . " track(s) to download:</p>";
foreach ($tracks as $track) {
echo "<div style='border: 1px solid #ccc; padding: 15px; margin: 10px 0; border-radius: 5px;'>";
echo "<h3>" . htmlspecialchars($track['title']) . "</h3>";
echo "<p><strong>Task ID:</strong> " . $track['task_id'] . "</p>";
echo "<p><strong>External URL:</strong> " . htmlspecialchars($track['audio_url']) . "</p>";
// Generate local filename
$localFilename = $track['task_id'] . '.mp3';
$localPath = $audioDir . '/' . $localFilename;
$localUrl = '/' . $audioDir . '/' . $localFilename;
echo "<p><strong>Local Path:</strong> $localPath</p>";
echo "<p><strong>Local URL:</strong> $localUrl</p>";
// Check if file already exists
if (file_exists($localPath)) {
echo "<p style='color: green;'>✅ File already exists locally</p>";
} else {
// Download the file
echo "<p>⏳ Downloading audio file...</p>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $track['audio_url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$audioData = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
echo "<p style='color: red;'>❌ Download failed: $error</p>";
} elseif ($httpCode !== 200) {
echo "<p style='color: red;'>❌ Download failed: HTTP $httpCode</p>";
} else {
// Save the file locally
$bytesWritten = file_put_contents($localPath, $audioData);
if ($bytesWritten === false) {
echo "<p style='color: red;'>❌ Failed to save file locally</p>";
} else {
echo "<p style='color: green;'>✅ Downloaded and saved: " . number_format($bytesWritten) . " bytes</p>";
}
}
}
// Update database with local URL
if (file_exists($localPath)) {
$updateStmt = $pdo->prepare("UPDATE music_tracks SET audio_url = ? WHERE id = ?");
$result = $updateStmt->execute([$localUrl, $track['id']]);
if ($result) {
echo "<p style='color: green;'>✅ Updated database with local URL</p>";
echo "<p><strong>New URL:</strong> <a href='$localUrl' target='_blank'>$localUrl</a></p>";
} else {
echo "<p style='color: red;'>❌ Failed to update database</p>";
}
}
echo "</div>";
}
}
// Show all tracks after update
echo "<h2>🎵 All Tracks After Update:</h2>";
$stmt = $pdo->prepare("SELECT * FROM music_tracks WHERE status = 'complete' ORDER BY created_at DESC");
$stmt->execute();
$allTracks = $stmt->fetchAll();
if (empty($allTracks)) {
echo "<p style='color: orange;'>⚠️ No complete tracks found</p>";
} else {
echo "<p style='color: green;'>✅ Found " . count($allTracks) . " complete track(s):</p>";
foreach ($allTracks as $track) {
echo "<div style='border: 1px solid #ccc; padding: 15px; margin: 10px 0; border-radius: 5px;'>";
echo "<h3>" . htmlspecialchars($track['title']) . "</h3>";
echo "<p><strong>Status:</strong> " . $track['status'] . "</p>";
echo "<p><strong>Audio URL:</strong> " . ($track['audio_url'] ? "<a href='" . htmlspecialchars($track['audio_url']) . "' target='_blank'>" . htmlspecialchars($track['audio_url']) . "</a>" : "None") . "</p>";
echo "<p><strong>Created:</strong> " . $track['created_at'] . "</p>";
echo "</div>";
}
}
echo "<h2>🔧 Quick Actions:</h2>";
echo "<p><a href='/library.php' style='background: #667eea; color: white; padding: 0.5rem 1rem; text-decoration: none; border-radius: 5px; margin-right: 1rem;'>📚 Go to Library</a>";
echo "<a href='/create.php' style='background: #48bb78; color: white; padding: 0.5rem 1rem; text-decoration: none; border-radius: 5px; margin-right: 1rem;'>🎵 Create New Music</a>";
echo "<a href='/dashboard.php' style='background: #764ba2; color: white; padding: 0.5rem 1rem; text-decoration: none; border-radius: 5px;'>🏠 Back to Dashboard</a></p>";
echo "<h2>💡 Security Benefits:</h2>";
echo "<ul>";
echo "<li>✅ Audio files stored locally on your server</li>";
echo "<li>✅ No exposure of API BOX URLs to clients</li>";
echo "<li>✅ Faster loading times (no external API calls)</li>";
echo "<li>✅ Better user experience</li>";
echo "</ul>";
?>