文件上传功能异常,疑似目录权限问题求助(附代码)
Hi there, let's break down how to fix your long-standing file upload issue, focusing on permission configuration tied to your existing code. First, let's spot key details in your code and then walk through actionable steps.
1. Fix Path Inconsistencies First
Looking at your code, there's a mismatch in upload paths that might be causing confusion (and silent failures):
- In your PDF processing code, you're using
FCPATH . "uploads/"(this points to the directory where your CodeIgniterindex.phplives, plus/uploads/) - In your upload config, you're using
../hctopses/uploads/(a relative path pointing up one directory, then intohctopses/uploads/)
These are two different directories! First, unify your paths to avoid uploading files to one location and trying to process them from another. Update your upload config to use the same absolute path as your processing code:
// Replace this relative path // $config['upload_path'] = '../hctopses/uploads/'; // With this absolute path (matches your PDF processing code) $config['upload_path'] = FCPATH . 'uploads/';
2. Set Correct Directory Permissions
Now that your path is consistent, let's fix the permissions for the uploads/ directory. Here's what you need to do:
Step 1: Identify Your Web Server User
First, figure out which user your web server runs as (this varies by server):
# For Apache ps aux | grep apache # For Nginx ps aux | grep nginx
You'll typically see users like www-data (Debian/Ubuntu) or apache (CentOS/RHEL).
Step 2: Set Directory Ownership and Permissions
Run these commands in your terminal (replace /path/to/your/project/uploads/ with the actual absolute path to your uploads folder, and www-data with your web server user):
# Change ownership of the uploads directory to the web server user chown -R www-data:www-data /path/to/your/project/uploads/ # Set directory permissions (755 allows read/write/execute for owner, read/execute for others) chmod -R 755 /path/to/your/project/uploads/
- Why 755? It's secure (avoids risky world-writable permissions like 777) and gives the web server user (owner) full access to upload and modify files, while others can only read and traverse the directory.
3. Check Permissions for External Commands
Your code uses exec("pdfinfo ...") and shell_exec("gs ...") to process PDFs. Make sure:
- The web server user can execute these tools. Verify with:
If these return paths (likesudo -u www-data which pdfinfo sudo -u www-data which gs/usr/bin/pdfinfo), the user has access. If not, install the tools or adjust their permissions. - The web server user has read access to uploaded PDFs (which they will if directory permissions are set correctly above).
4. Fix Code Issues That Exacerbate Problems
Beyond permissions, a couple of tweaks will make your uploads more reliable:
- Avoid Temp File Conflicts: Your code uses
temp.pdfwhich will be overwritten if two users upload simultaneously. Use a unique temp filename instead:$temp_file = FCPATH . "uploads/temp_" . uniqid() . ".pdf"; shell_exec("mv " . $tech_file_path . " " . $temp_file); $command1 = "/usr/bin/gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -o " . $tech_file_path . " " . $temp_file; // Clean up the temp file after processing unlink($temp_file); - Add Better Error Logging: Instead of just echoing errors, log them to CodeIgniter's logs to debug easier:
if( ! $this->upload->do_upload('tech_justification')) { $error = $this->upload->display_errors(); $err_msg = "<b> Error </b> uploading Tech Just : " . $error; log_message('error', 'Tech Justification Upload Error: ' . $error); // Add this line } - Verify PHP Upload Settings: Check your
php.inifile to ensure these are set:file_uploads = Onupload_max_filesize = 5M(matches yourmax_sizeof 5120 KB)post_max_size = 8M(should be larger thanupload_max_filesize)
Final Checklist
- Unify your upload paths to use
FCPATH . 'uploads/'everywhere - Set correct ownership and permissions for the uploads directory
- Verify the web server user can access
pdfinfoandgs - Fix temp file conflicts and add error logging
- Double-check PHP's upload configuration
Following these steps should resolve your permission-related upload issues. If you still run into problems, check CodeIgniter's error logs (found in application/logs/) for specific messages that can point to the root cause.
内容的提问来源于stack exchange,提问作者Etuor

