PHP CodeIgniter上传文件至内部服务器失败,提示路径无效求助
Hey there! As someone who's stumbled through similar CodeIgniter upload issues when starting out, I totally get how frustrating this error can be. Let's break down why this is happening and fix it step by step.
Common Reasons & Solutions
1. Your Absolute Path is Missing a Leading Slash
Linux servers require absolute paths to start with a / (the root directory marker). Your current path var/www/vhost/halalquest.id/data/admin/place/ is being treated as a relative path (relative to your CodeIgniter project's root folder), which almost certainly doesn't exist.
Fix: Add the leading slash to make it a valid absolute path:
$config['upload_path'] = '/var/www/vhost/halalquest.id/data/admin/place/';
2. Directory Permissions Are Incorrect
Even if the path is correct, CodeIgniter needs read/write access to the upload directory. Linux enforces strict permission rules, so:
- Ensure the directory has at least
755permissions (read/write/execute for the owner, read/execute for others) - Make sure the directory is owned by your web server's user (usually
www-datafor Apache/Nginx)
Check & Fix via SSH:
# Verify the directory exists and check current permissions ls -ld /var/www/vhost/halalquest.id/data/admin/place/ # Set correct permissions chmod 755 /var/www/vhost/halalquest.id/data/admin/place/ # Set correct owner (replace www-data with your server's web user if needed) chown www-data:www-data /var/www/vhost/halalquest.id/data/admin/place/
3. The Directory Doesn't Exist (or Has a Typo)
Double-check that every folder in your path exists exactly as you wrote it. Linux is case-sensitive, so Place/ vs place/ would be treated as two completely different directories.
If the directory is missing, create it with:
mkdir -p /var/www/vhost/halalquest.id/data/admin/place/
4. Use CodeIgniter's Built-in Constants for Reliability
Hard-coded absolute paths can break if you ever move your project. For paths relative to your project root, use CodeIgniter's FCPATH constant (it points directly to your index.php directory). For example, your working local path could be rewritten as:
$config['upload_path'] = FCPATH . 'assets/images/product/';
Final Check
After fixing the path and permissions, test your upload again. If it still fails, enable CodeIgniter's error logging (check application/config/config.php and set log_threshold to 2) to get more detailed debug info.
内容的提问来源于stack exchange,提问作者mochammad rizky

