duanluo9369 2014-05-23 12:54 采纳率: 100%
浏览 250
已采纳

require_once():无法打开所需的'...'(但文件存在)

When I use require_once I get the following error:

"PHP Fatal error: require_once(): Failed to opening required '/Application/Rules/required_file.class.php'"

My directory structure is:

  • Project
    • Application
      • Rules
        • required_file
    • Public
      • Users
        • file

"file" is where I use require_once:

require_once('/Application/Rules/required_file.class.php');

Why the error? Can anyone help me with the solution?

  • 写回答

3条回答 默认 最新

  • douwen7475 2014-05-23 12:59
    关注

    If I got you right, you are calling the

    require_once('/Application/Rules/required_file.class.php');
    

    within the file? That does not work because require_once always relates to the current folder, as long as you do not use absolute paths.

    Instead you need to call

    require_once('../../Application/Rules/required_file.class.php');
    

    because the file you want to include is not in the same folder as your file.

    ../ goes back one folder in the hierarchy.

    ../../ therefore goes back from /Users to /Public and then to /Project, form where you then can go to /Application.


    I think this article might explain the difference between relative and absolute paths quite well.

    The relative path points to a file or directory in relation to where the present file is located.

    The absolute path is the "full path" from the webserver point of view. It is the path that contains the document root. For example /var/www/mydomain.com/.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?