Mj1992
Mj1992

Reputation: 3504

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 . Every things works fine till here.

I've another script named Upload.php from which data gets uploaded. 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

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 :

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

In Profile.php :

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

In Profile_Control.php:

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

Document Structure:

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

Upvotes: 3

Views: 13057

Answers (2)

Lix
Lix

Reputation: 47966

Instead of using relative paths (../), why don't you try giving absolute paths relative to your $_SERVER['DOCUMENT_ROOT'] location. 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.

user1280616 also makes a valid point with regard to testing the existence of a file prior to including it. Perhaps you should consider implementing that as well.

Upvotes: 8

NullPoiиteя
NullPoiиteя

Reputation: 57322

you can use the php file_exists() function to make sure that file is included if it is than do your stuff

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");

Upvotes: 1

Related Questions