3

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
Shikkediel
  • 5,195
  • 16
  • 45
  • 77
Mj1992
  • 3,404
  • 13
  • 63
  • 102
  • One often runs into this error, and to quickly troubleshoot it, follow these steps : https://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 15:53
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Apr 30 '16 at 14:23

2 Answers2

8

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.

Community
  • 1
  • 1
Lix
  • 47,311
  • 12
  • 103
  • 131
  • While the answer marked at correct is helpful I must say it was this answer that solved my problem. I tried both absolute and relative paths and both failed without first getting the document root and using that in the path. – vmex151 Jul 23 '13 at 13:23
  • There is now a troubleshooting checklist for this frequent error here : https://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 15:53
1

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");
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143