Jump to content

PHP Include Path Error


mufasil
Go to solution Solved by mufasil,

Recommended Posts

Dear Sir,

 

I am building a small framework and going to check on my local computer by using "XAMPP Server" so i have 2 folder and 5 file the hierarchy is:

- inc

  - config.php (Contains all configuration of website like, Directory Separator, Include Path .. etc.)

  - autoload.php 

-classes

  - Url.php      (Class)

  - Core.php   (Class)

- index.php

 

when i try to run this application i got following Error:

 

 

Warning: require_once(Core.php): failed to open stream: No such file or directory in C:\xampp\htdocs\completed\inc\autoload.php on line 7

Fatal error: require_once(): Failed opening required 'Core.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\completed\inc\autoload.php on line 7

 

I have also tried to change settings of include_path php.ini changes are following:

1. remove ',;' from include_path

2. try to change to this '\php\PEAR'

3. put ';' in starting og php_include_path

 

my current default setting of php include is:

 

;;;;;;;;;;;;;;;;;;;;;;;;;

; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;
; Windows: "\path1;\path2"
include_path=".;C:\xampp\php\PEAR"
;

 

Now what i see after doing the changes in php.ini file.

When i add ';' in starting of include_path website seems to work but files locations are changed and file not able to access properly means the URL looks like this,

URL: localhost/images/abc.png

The proper URL is:

Proper URL: localhost/project-name/images/abc.png

 

Kindly tell me what i do to solve this problem 

 

Thanks

 

Link to comment
Share on other sites

Two separate problems.

 

First one: use absolute paths whenever possible and reserve the include_path for external code (not bundled with the website's code).

- It'd be great if you could have the URLs mirror the final, production URLs. So if the correct production path to the image is "/images/abc.png" then you should have that happening in your dev environment as well.

- If you have just the one project then move all the project-name/ files into the htdocs folder and do away with the subdirectory.

- If you have more than one, use separate domain names and separate VirtualHost entries for each. Then the full URLs would be (example:) http://project-name.localhost/images/abc.png. You'll need to edit your Windows hosts file for that to work, creating an entry inside it for each subdomain, but it's worth it.

- In your code, $_SERVER["DOCUMENT_ROOT"] will now always point to the root of your website. You can then create absolute paths with code like

require_once($_SERVER["DOCUMENT_ROOT"] . "/classes/Core.php");

The second one: it would be solved with the stuff above regarding the URLs and such.

If you can't do that then it gets complicated and you have to deal with relative paths in CSS and such; right now you're using "/images/abc.png" with a leading slash and instead you'd have to do "images/abc.png" without the slash. It gets worse when you need to go up a directory or two because you'd have to use URLs like "../../images/abc.png".

Edited by requinix
Link to comment
Share on other sites

Thanks requinix for your detailed reply.

I am 100% agree with your reply, i think i can't able to describe my problem properly, Basically i am learning php i have great knowledge about procedural PHP, now i am using "Udemy.com" tutorials to learn OOPS and work with Framework, so i am following my Udemy teacher what he is doing, and i think the tutor is doing perfect coding because when i run that coding on my hosting that works fine, so i am sharing my whole coding please check and tell me the solution, The only problem is that when i tried to access my index.php file the following error occurs.

 

My Project Path: C://xampp/htdocs/project-name

 

 

 

Warning: require_once(Core.php): failed to open stream: No such file or directory in C:\xampp\htdocs\completed\inc\autoload.php on line 7

Fatal error: require_once(): Failed opening required 'Core.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\completed\inc\autoload.php on line 7

 

Project Coding:

 

1. /inc/autoload.php

 

<?php

require_once('config.php');
function __autoload($class_name)
{
$class = explode("_",$class_name);
$path = implode("/",$class).".php";
require_once($path);
}

 

2. /inc/config.php

 

 

<?php

if(!isset($_SESSION)) {
    session_start();
}
 
// site domain name with http
defined("SITE_URL")
|| define("SITE_URL", "https://".$_SERVER['SERVER_NAME']);
 
// directory separator
defined("DS")
|| define("DS", DIRECTORY_SEPARATOR);
 
// root path
defined("ROOT_PATH")
|| define("ROOT_PATH", realpath(dirname(__FILE__) . DS."..".DS));
 
// classes folder
defined("CLASSES_DIR")
|| define("CLASSES_DIR", "classes");
 
// pages directory
defined("PAGES_DIR")
|| define("PAGES_DIR", "pages");
 
// modules folder
defined("MOD_DIR")
|| define("MOD_DIR", "mod");
 
// inc folder
defined("INC_DIR")
|| define("INC_DIR", "inc");
 
// templates folder
defined("TEMPLATE_DIR")
|| define("TEMPLATE_DIR", "template");
 
// emails path
defined("EMAILS_PATH")
|| define("EMAILS_PATH", ROOT_PATH.DS."emails");
 
// catalogue images path
defined("CATALOGUE_PATH")
|| define("CATALOGUE_PATH", ROOT_PATH.DS."media".DS."catalogue");
 
// add all above directories to the include path
ini_set('include_path',implode(PATH_SEPARATOR, array(
    realpath(ROOT_PATH.DS.CLASSES_DIR),
    realpath(ROOT_PATH.DS.PAGES_DIR),
    realpath(ROOT_PATH.DS.MOD_DIR),
    realpath(ROOT_PATH.DS.INC_DIR),
    realpath(ROOT_PATH.DS.TEMPLATE_DIR),
    ini_get('include_path')
)));

 

3. /classes/Core.php

 

<?php

class Core
{
public function run()
{
ob_start();
        require_once(Url::getPage());
        ob_get_flush();
}
}

 

4. /classes/Url.php

 

<?php

class Url
{
    public static $_page = "page";
    public static $_folder = PAGES_DIR;
    public static $_prams = array();
    public static function getParam($par)
    {
        return isset($_GET[$par]) && $_GET[$par] != "" ? $_GET[$par] : null;
    }
    public static function cPage() // yeh current page return karta hai.
    {
        return isset($_GET[self::$_page]) ? $_GET[self::$_page] : 'index';
    }
    public static function getPage()
    {
        $page = self::$_folder.DS.self::cPage().".php";
        $error = self::$_folder.DS."error.php";
        return is_file($page) ? $page : $error;
    }
    public static function getAll()
    {
        if(!empty($_GET))
        {
            foreach($_GET as $key => $value)
            {
                if(!empty($value))
                {
                    self::$_prams[$key] = $value;
                }
            }
        }
    }
}

5. /pages/index.php

 

<?php

require_once('_header.php');
require_once('_footer.php');
?>

6. /pages/error.php

 

<?php

require_once('_header.php');
echo("<h1>Error</h1>");
require_once('_footer.php');
?>

6. index.php

 

<?php

require_once('inc/autoload.php');
$core =  new Core();
$core->run();
?>

 

Above are the codes for whole project when i run this i got following error, How can i resolve, Kindly Help.

 

 

Warning: require_once(Core.php): failed to open stream: No such file or directory in C:\xampp\htdocs\completed\inc\autoload.php on line 7

Fatal error: require_once(): Failed opening required 'Core.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\completed\inc\autoload.php on line 7

 

Thanks

Link to comment
Share on other sites

All your classes are located in the classes folder. 

 

In config.php there is a constant called CLASSES_DIR . This contains the path to where your classes are located. Prepending CLASSES_DIR to $path should now allow the autoloader to find the class files  within your classes folder

 

require_once($path);  should be 

require_once(CLASSES_DIR . DS . $path); 

//OR

require_once(ROOT_PATH . CLASSES_DIR . DS . $path);
Edited by Ch0cu3r
Link to comment
Share on other sites

Thanks again for your reply,

 

I have tried both one bu one the error seems to be resolve, but new problem occurs that page not loading css while when i upload it on my Web hosting it works fine, i am attaching screenshots of both localhost and hosting.

 

Screenshot #1 - Localhost:

http://s30.postimg.org/rl48kktxd/PHP_problem.png

 

Screenshot #2 - Webhosting:

http://s30.postimg.org/phttcwu4h/PHP_Real_page.png

 

Please check i have echo the set_include_path in Screenshot #1.

 

Thanks

Edited by mufasil
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.