简体   繁体   English

无法打开流没有这样的文件或目录。 PHP

[英]Failed to open stream no such file or directory. php

I've a file Profile.php which includes Profile_Control.php and the profile_control includes Profile_Model.php . 我有一个文件Profile.php ,其中包含Profile_Control.phpprofile_control包含Profile_Model.php Every things works fine till here. 在这里,每件事情都很好。

I've another script named Upload.php from which data gets uploaded. 我还有另一个名为Upload.php脚本,从中可以上传数据。 This Upload.php also includes Profile_Control.php and as you know Profile_Control includes Profile_Model.php .Now I dont know why it is giving such an error.When the Profile.php loads it works fine but when I upload the data it says 这个Upload.php还包括Profile_Control.php ,你知道Profile_Control包含了Profile_Model.php 。现在我不知道它为什么会出现这样的错误。当Profile.php加载它工作正常但是当我上传数据时它说

Warning: include(../Model/Profile_Model.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\php\gagster\Control\Profile_Control.php on line 4

In Upload.php : 在Upload.php中:

include_once("../../Control/Profile_Control.php");

In Profile.php : 在Profile.php中:

include_once("../Control/Profile_Control.php");

In Profile_Control.php: 在Profile_Control.php中:

include_once("../Model/Profile_Model.php");

Document Structure: 文件结构:

 +-Control/
 |  |    
 |  +---- Profile_Control.php
 |
 +-Model/
 |  |
 |  +---- Profile_Model.php
 |
 +-Other/
    |
    +-- Upload/
           |
           +---- Upload.php

Instead of using relative paths ( ../ ), why don't you try giving absolute paths relative to your $_SERVER['DOCUMENT_ROOT'] location. 而不是使用相对路径( ../ ),为什么不尝试给出相对于$_SERVER['DOCUMENT_ROOT']位置的绝对路径。 I usually define a constant to help with readability - 我通常定义一个常量来帮助提高可读性 -

define('_root',$_SERVER['DOCUMENT_ROOT']);

include(_root.'/Control/Profile_Control.php');

Now you can place the same line of code in each file you want to include Profile_Control.php in. 现在,您可以在要包含Profile_Control.php每个文件中放置相同的代码行。

user1280616 also makes a valid point with regard to testing the existence of a file prior to including it. user1280616在包含文件之前测试文件的存在性也是有效的。 Perhaps you should consider implementing that as well. 也许您应该考虑实施它。

you can use the php file_exists() function to make sure that file is included if it is than do your stuff 你可以使用php file_exists()函数来确保包含文件,如果它不是你的东西

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

and use the 并使用

defined('root') ? null : define('root', "url of your site "  ); 

than

include_once(root."rest of address");

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

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