You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

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:

Troubleshooting Steps for "File not found" with Existing PHP Files
  • Double-check path case sensitivity
    Most Linux/Unix-based web servers are case-sensitive. If your code references include('MyScript.php') but the actual file is named myscript.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.php and 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 DocumentRoot points to the correct website root directory. Also check if a .htaccess file has incorrect rewrite rules that are redirecting requests to non-existent locations.
    • For Nginx: Verify the root directive in your server block maps to your file directory, and that location blocks aren't misrouting requests.
  • Check file and directory permissions
    Even if the file exists, the server process (e.g., www-data on Linux) might lack read access. Run this command to check permissions:

    ls -l /path/to/your/php/file.php
    

    Ensure the file has at least 644 permissions, and parent directories have 755 permissions, so the server can read the file.

  • Clear browser and server-side caches
    Sometimes browsers cache old error pages—try a hard refresh with Ctrl+Shift+R or 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 your php.ini file. This is often the fastest way to pinpoint the root cause.

内容的提问来源于stack exchange,提问作者Chaudhry Yousuf

火山引擎 最新活动