简体   繁体   English

致命错误:require():需要打开失败

[英]Fatal error : require(): Failed opening required

So, I have a directory and the sub like this 所以,我有一个目录和这样的子目录

+task
  +Include  
    +vendor  
     - autoload.php

  +Theme  
    +action
     - doInsert.php
    +objects
     - CardAccessor.php

And the error appeared when I try to call the autoload.php in CardAccessor.php 当我尝试在CardAccessor.php调用autoload.php时出现错误

doInsert.php doInsert.php

require_once '../objects/CardAccessor.php';

CardAccessor.php CardAccessor.php

require '/../include/vendor/autoload.php';

The error said : 错误说:

Warning: require(/../include/vendor/autoload.php): failed to open stream: No such file or directory Fatal error: require(): Failed opening required '/../include/vendor/autoload.php' 警告:require(/../ include / vendor / autoload.php):无法打开流:没有此类文件或目录致命错误:require():无法打开所需的'/../include/vendor/autoload.php'

require '/../include/vendor/autoload.php' won't work, you'll need to take the first slash off. require '/../include/vendor/autoload.php'不起作用,您需要先require '/../include/vendor/autoload.php'斜线。 As it is written, the first slash tells it to go to the root directory, then go up one from there (which it can't do, it's already at the root). 在编写时,第一个斜杠告诉它进入根目录,然后从那里向上上升(它不能执行,因为它已经在根目录了)。

Given your directory structure, I believe that require '../../include/vendor/autoload.php' should work. 给定您的目录结构,我认为require '../../include/vendor/autoload.php'应该可以工作。

When you do an include , the path is always relative to the file that include the file. 当您执行include ,路径始终相对于包含该文件的文件。

What you need to put is require '../../Include/vendors/autoload.php' or set the full path like require '/my/project/path/task/Include/vendor/autoload.php' . 您需要输入的是require '../../Include/vendors/autoload.php'或设置完整路径,例如require '/my/project/path/task/Include/vendor/autoload.php' path/ require '/my/project/path/task/Include/vendor/autoload.php'

This is revelant to your problem : PHP - Relative paths "require" 这与您的问题有关:PHP- 相对路径“ require”

you need to define a config 您需要定义一个配置

if this is linux unix use 如果这是linux unix使用

$baseDir = "/base/dir"; //to define where in the root filesys where youre scripts and assets live

then use 然后使用

$baseDir."/task/Include" or 
$baseDir."/task/Theme/objects/CardAccessor.php" or 
$baseDir."/task/include/vendor/autoload.php"

or you can create 或者你可以创建

$sourceDir = $baseDir."/task/";

this is also used for portability, if you decide to move the source you can modify the config; 这也用于可移植性,如果您决定移动源,则可以修改配置; in addition, this is an efficient way to include or require files in your scripts. 此外,这是在脚本中包含或要求文件的有效方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM