Ubuntu环境下PHP无法显示图片问题求助
Let's break down why your image script works on Windows but fails on Ubuntu, and fix it step by step. The "image contains errors" message almost always means binary image data is being corrupted by extra text (like PHP errors) in the response.
1. Fix the Undefined Function (Biggest Culprit)
Looking at your code, you're using str_substract("screenshot-", $requestImage);—this function doesn't exist in PHP! That's causing PHP to spit out an error message directly into the response, which mixes with the image binary data and breaks everything.
To remove the "screenshot-" prefix correctly, use either str_replace or substr:
// Option 1: Replace the prefix with nothing $imageName = str_replace("screenshot-", "", $requestImage); // Option 2: Cut off the first 11 characters ("screenshot-" is exactly 11 chars) $imageName = substr($requestImage, 11);
2. Add Critical Input Validation & Error Handling
Your current script skips basic checks that can cause errors or security risks. Here's an updated version with safeguards:
<?php $urlFolder = "./images/screenshots/"; // Check if the 'img' parameter was even sent if (!isset($_GET['img'])) { http_response_code(400); exit("Missing image parameter"); } $requestImage = $_GET['img']; // Remove the prefix safely $imageName = str_replace("screenshot-", "", $requestImage); // Block path traversal attacks (e.g., someone trying to load ../secret.png) $imageName = basename($imageName); $imgpath = $urlFolder . $imageName; // Make sure the file exists and is readable if (!file_exists($imgpath) || !is_readable($imgpath)) { http_response_code(404); exit("Image not found"); } // Validate the file is actually an image $imageInfo = getimagesize($imgpath); if (!$imageInfo) { http_response_code(400); exit("Not a valid image file"); } // Set proper headers header("Content-Type: " . $imageInfo['mime']); header("Content-Length: " . filesize($imgpath)); // Output the image $fileHandle = fopen($imgpath, 'rb'); fpassthru($fileHandle); fclose($fileHandle); exit;
3. Fix File Permissions (Ubuntu is Strict About This)
On Ubuntu, Nginx runs as the www-data user. If this user can't read your images, the script will fail silently (or with hidden errors). Run these commands to fix permissions:
# Make www-data the owner of your images directory sudo chown -R www-data:www-data /home/www/images/screenshots/ # Set read/write permissions correctly sudo chmod -R 755 /home/www/images/screenshots/ sudo chmod 644 /home/www/images/screenshots/*.png
4. Double-Check Nginx Configuration
- Confirm your
root /home/www;is correct—this means./images/screenshots/maps to/home/www/images/screenshots/on your server. - Your image location block currently falls back to
index.phpwhen an image isn't found, but your script isverImagem.php. This won't affect your current workflow (since you're accessing the script directly), but if you ever want direct image URLs to use your script, update that line to:try_files $uri $uri/ /verImagem.php$is_args$args;
5. Disable PHP Error Output in Production
If PHP is still outputting errors to the browser, they'll keep corrupting your image data. Edit your php.ini (usually at /etc/php/7.0/fpm/php.ini):
display_errors = Off log_errors = On error_log = /var/log/php7.0-fpm.log
Then restart PHP-FPM to apply changes:
sudo service php7.0-fpm restart
After applying these fixes, your script should display images correctly on Ubuntu.
内容的提问来源于stack exchange,提问作者thevankk

