PHP CodeIgniter视图加载WAMP www外本地音频资源失败的解决方案咨询
Alright, let's tackle that "unable to load local resources" error you're hitting when trying to serve audio from D:\Recs (outside your WAMP www directory) via CodeIgniter. Browsers block direct filesystem paths like D:\Recs\file.mp3 for security, so we need to proxy or expose those files through your web server properly. Here are three solid solutions:
1. Use a CodeIgniter Controller as a File Proxy (Most Flexible)
This approach lets you control access to your audio files and works without major server config changes.
Step 1: Create an Audio Controller
Make a new controller file application/controllers/Audio.php with this code:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Audio extends CI_Controller { public function stream($filename) { // Sanitize the filename to prevent path traversal attacks if (strpos($filename, '../') !== false || !preg_match('/\.(mp3|wav|ogg|m4a)$/i', $filename)) { show_404(); return; } // Define your audio directory path (use forward slashes for cross-platform compatibility) $audio_path = 'D:/Recs/' . $filename; // Check if the file exists if (!file_exists($audio_path)) { show_404(); return; } // Set proper HTTP headers for audio streaming $mime = mime_content_type($audio_path); header('Content-Type: ' . $mime); header('Content-Length: ' . filesize($audio_path)); header('Content-Disposition: inline; filename="' . $filename . '"'); // Stream the file to the browser readfile($audio_path); exit; } }
Step 2: Update Your View
In your HTML view, reference the audio file through the controller's stream method:
<audio controls> <source src="<?php echo base_url('audio/stream/my-audio-file.mp3'); ?>" type="audio/mpeg"> Your browser does not support the audio element. </audio>
Don't forget to set a valid base_url in application/config/config.php (e.g., $config['base_url'] = 'http://your-public-domain.com/';).
2. Configure a WAMP Virtual Host (Direct Server Access)
If you prefer to expose the D:\Recs directory directly via Apache, set up a virtual host:
Step 1: Enable Virtual Hosts in Apache
Open your WAMP Apache httpd.conf file (usually in C:\wamp64\bin\apache\apacheX.X.X\conf) and uncomment these lines if they're commented out:
LoadModule rewrite_module modules/mod_rewrite.so Include conf/extra/httpd-vhosts.conf
Step 2: Add the Virtual Host
Edit httpd-vhosts.conf (in C:\wamp64\bin\apache\apacheX.X.X\conf\extra) and add this block:
<VirtualHost *:80> ServerName audio.yourdomain.com # Replace with your actual domain (or local test name like audio.local) DocumentRoot "D:/Recs" <Directory "D:/Recs"> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted # Restrict access to only audio file types for security <FilesMatch "\.(mp3|wav|ogg|m4a)$"> Require all granted </FilesMatch> </Directory> </VirtualHost>
Step 3: Update Hosts File (For Local Testing)
If testing locally, edit your Windows hosts file (C:\Windows\System32\drivers\etc\hosts) and add:
127.0.0.1 audio.yourdomain.com
Step 4: Use the Virtual Host in Your View
Now you can link directly to the audio files via the virtual host:
<audio controls> <source src="http://audio.yourdomain.com/my-audio-file.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>
Important: Make sure the WAMP service user (typically www-data or your local Windows user) has read permissions for D:\Recs.
3. Create a Symbolic Link to WAMP www Directory (Quick Fix)
For a simple workaround, create a symbolic link from your WAMP www directory to D:\Recs:
Step 1: Create the Symbolic Link
Open Command Prompt as Administrator and run:
mklink /D "C:\wamp64\www\recs" "D:\Recs"
Replace C:\wamp64\www with your actual WAMP www path.
Step 2: Allow Symlinks in Apache
Ensure Apache follows symbolic links by adding/updating this line in httpd.conf:
Options +FollowSymLinks
Step 3: Link to Files in Your View
Now you can reference files through the symlink:
<audio controls> <source src="<?php echo base_url('recs/my-audio-file.mp3'); ?>" type="audio/mpeg"> Your browser does not support the audio element. </audio>
Note: This is the quickest fix but less flexible for access control, and may cause issues if you migrate your server later.
内容的提问来源于stack exchange,提问作者user4841621

