![]() 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/public_html/ |
<?php
// Download and store ALL external audio files locally
require_once 'config/database.php';
$pdo = getDBConnection();
// Create audio directory if needed
$audioDir = 'audio_files/';
if (!is_dir($audioDir)) {
mkdir($audioDir, 0755, true);
echo "<p style='color: green;'>✅ Created audio directory</p>";
}
// Find all tracks with external URLs
$stmt = $pdo->prepare("
SELECT * FROM music_tracks
WHERE status = 'complete'
AND audio_url IS NOT NULL
AND audio_url != ''
AND (
audio_url LIKE 'http%'
OR audio_url LIKE '%api.box%'
OR audio_url LIKE '%apiboxfiles.erweima.ai%'
)
AND task_id IS NOT NULL
AND task_id != ''
ORDER BY created_at DESC
");
$stmt->execute();
$tracks = $stmt->fetchAll();
echo "<h1>Download All External Audio Files</h1>";
echo "<p><strong>Found " . count($tracks) . " tracks with external URLs</strong></p>";
$downloaded = 0;
$skipped = 0;
$failed = 0;
$updated = 0;
foreach ($tracks as $track) {
$audioUrl = $track['audio_url'];
$taskId = $track['task_id'];
$trackId = $track['id'];
$title = $track['title'];
echo "<hr>";
echo "<h3>Track: {$title} (ID: {$trackId})</h3>";
echo "<p>Task ID: {$taskId}</p>";
echo "<p>Current URL: " . htmlspecialchars($audioUrl) . "</p>";
// Generate local filename
$extension = pathinfo(parse_url($audioUrl, PHP_URL_PATH), PATHINFO_EXTENSION) ?: 'mp3';
$filename = "{$taskId}.{$extension}";
$localPath = $audioDir . $filename;
$webPath = '/audio_files/' . $filename;
// Check if file already exists
if (file_exists($localPath)) {
echo "<p style='color: green;'>✅ File already exists: {$webPath}</p>";
$skipped++;
// Update database if still pointing to external URL
if ($track['audio_url'] !== $webPath) {
$updateStmt = $pdo->prepare("UPDATE music_tracks SET audio_url = ? WHERE id = ?");
if ($updateStmt->execute([$webPath, $trackId])) {
echo "<p style='color: green;'>✅ Updated database to use local file</p>";
$updated++;
}
}
continue;
}
// Download the file
echo "<p>📥 Downloading...</p>";
$context = stream_context_create([
'http' => [
'timeout' => 300,
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'follow_location' => true
]
]);
$audioContent = @file_get_contents($audioUrl, false, $context);
if ($audioContent === false) {
echo "<p style='color: red;'>❌ Download failed!</p>";
$failed++;
continue;
}
$fileSize = strlen($audioContent);
echo "<p>✅ Downloaded " . number_format($fileSize) . " bytes</p>";
// Save the file
if (file_put_contents($localPath, $audioContent, LOCK_EX)) {
chmod($localPath, 0644);
echo "<p style='color: green;'>✅ Saved to: {$webPath}</p>";
$downloaded++;
// Update database to use local path
$updateStmt = $pdo->prepare("UPDATE music_tracks SET audio_url = ? WHERE id = ?");
if ($updateStmt->execute([$webPath, $trackId])) {
echo "<p style='color: green; font-weight: bold;'>✅ Database updated!</p>";
$updated++;
} else {
echo "<p style='color: orange;'>⚠️ File saved but database update failed</p>";
}
} else {
echo "<p style='color: red;'>❌ Failed to save file!</p>";
$failed++;
}
// Add a small delay to avoid overwhelming the server
usleep(500000); // 0.5 second delay
}
echo "<hr>";
echo "<h2>Summary</h2>";
echo "<p><strong>Downloaded:</strong> {$downloaded}</p>";
echo "<p><strong>Skipped (already exists):</strong> {$skipped}</p>";
echo "<p><strong>Failed:</strong> {$failed}</p>";
echo "<p><strong>Database Updated:</strong> {$updated}</p>";
?>