0

I am using a php framework (not a common one) and I am running into an issue. Whenever I add a sub-folder, my framework cannot be found. For instance, here is what my folder structure looks like.

+ = file

-account

-classes

-core

+index

+register

I have an init file within my core folder that works perfectly fine with files within the root, but when I am in files with the account folder (files for when logged in) it throws errors.

I have the following in my init file to call the classes.

spl_autoload_register(function($class) {
        require_once 'classes/' . $class . '.php';
    });

    require_once 'functions/sanitize.php';

So normally I do this to call the init file:

  require_once 'core/init.php';

I have tried doing this

  require_once '/core/init.php';

I get this error:

Warning: require_once(/core/init.php): failed to open stream: No such file or directory

Then when I do this

require_once 'http://sitename.com/core/init.php';

The init file works, but then the path to all of my classes does not work.

Do I need to do something with my init file, specifically these lines:

   spl_autoload_register(function($class) {
        require_once 'classes/' . $class . '.php';
    });

    require_once 'functions/sanitize.php';

To allow it to work for root files and sub-folder files?

enter image description here

Becky
  • 2,283
  • 2
  • 23
  • 50
  • `/core` is an absolute path; it looks like you want a relative one like `../core`. – mbethke Oct 14 '16 at 04:17
  • Possible duplicate of [PHP - Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Oct 15 '16 at 12:01

1 Answers1

0

If I understood correctly you are trying to require the core/init.php file from withing an file inside the classes directory? If so try using

require_once __DIR__ . '/../core/init.php';

The magic constant __DIR__ will refer to the current directory and the ../ will refer to its parent directory where the core directory is located.

Martins.A
  • 130
  • 2
  • 10
  • I am trying to require it within a sub-folder. The classes and core folders are folders in the root....just like the accounts folder (where the files I am trying to run this in). – Becky Oct 14 '16 at 01:46
  • So, if you are in the folder root/accounts/file.php, if you use the code above inside file.php it will point to root/core/init.php. Is is not working? Also the init file you mentioned with the sp_autoload function is inside the core directory? If so you should also probably make the path relative on that function like so `require_once '../classes/' . $class . '.php';` – Martins.A Oct 14 '16 at 01:59
  • I tried making the change you just recommended within my init class and get this error.. `Fatal error: Class 'Session' not found in`. So it is not making it into the classes folder it looks like. – Becky Oct 14 '16 at 02:04
  • Ok, seems like I got your folder and files structure wrong... It is a bit confusing in your question, could you try specifying better your folder / files structure? Maybe add an screenshot of the file tree, and specify which files are you referring to in each of the code snippets? – Martins.A Oct 14 '16 at 02:15
  • I added an image. The init file within the core folder works for the root files, such as the index/register. I am trying to get it to work within the accounts folder. – Becky Oct 14 '16 at 02:18
  • When you said: So normally I do this to call the init file: `require_once 'core/init.php';` that is from a file in the root folder, like the register or index right? And when you said I have tried doing this `require_once '/core/init.php';` it is from within a file inside the classes directory right? If so, my original answer should work correctly. I edited the answer, had forgotten to include a slash after the __DIR__ – Martins.A Oct 14 '16 at 02:31
  • No I am now trying to call the init file from a file within the accounts folder. The calling of the init file works, but the classes do not work. This was the main issue of my question. – Becky Oct 14 '16 at 02:41
  • Actually, now I think the classes are working, but for some reason within the db class, it throws these two errors. `Notice: Array to string conversion in` and `Warning: PDO::__construct() expects parameter 2 to be string, array given` for this line `$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));` Which works outside of the accounts folder. – Becky Oct 14 '16 at 02:48