PHP开发求助:文件存于目录却触发File not found错误
Hey there! Sorry to hear you're stuck with this "File not found" error even though your PHP file is definitely in the specified directory. Let's walk through some common fixes that usually resolve this frustrating issue:
Double-check path case sensitivity
Most Linux/Unix-based web servers are case-sensitive. If your code referencesinclude('MyScript.php')but the actual file is namedmyscript.php, the server will fail to locate it. Verify that every character in your file path matches the actual file/directory names exactly, including uppercase/lowercase.Test absolute paths instead of relative paths
Relative paths can be tricky if PHP's working directory isn't what you expect. For example, if you're in/var/www/html/app/index.phpand use../config/db.php, but the server's working directory is/var/www/html, the path breaks. Try using absolute paths to eliminate this ambiguity:// First, print your current script's directory to confirm context echo "Current directory: " . __DIR__; // Then use absolute path to include your file include(__DIR__ . '/../config/db.php');Inspect server configuration (Apache/Nginx)
- For Apache: Confirm your
DocumentRootpoints to the correct website root directory. Also check if a.htaccessfile has incorrect rewrite rules that are redirecting requests to non-existent locations. - For Nginx: Verify the
rootdirective in your server block maps to your file directory, and thatlocationblocks aren't misrouting requests.
- For Apache: Confirm your
Check file and directory permissions
Even if the file exists, the server process (e.g.,www-dataon Linux) might lack read access. Run this command to check permissions:ls -l /path/to/your/php/file.phpEnsure the file has at least
644permissions, and parent directories have755permissions, so the server can read the file.Clear browser and server-side caches
Sometimes browsers cache old error pages—try a hard refresh withCtrl+Shift+Ror open the page in incognito mode. On the server side, tools like OPcache might be caching outdated path info; restart your web server or temporarily disable OPcache to test.Review PHP error logs
PHP's error log will give you precise details about what's going on—like the exact path it's trying to access, or permission denials. Logs are usually located in/var/log/php/(Linux) or specified in yourphp.inifile. This is often the fastest way to pinpoint the root cause.
内容的提问来源于stack exchange,提问作者Chaudhry Yousuf




