Question

I am trying to clean up my website folder structure. I have too many processing scripts in the root directory. The reason is because they won't work if I move them to a sub folder and call them from there.

For eg.

root directory
  - images folder
  - css folder
  - js folder
  - includes folder
  - admin folder
index.php
ajax.php 

Say I move the ajax.php to includes folder, it will look like this.

root directory
  - images folder
  - css folder
  - js folder
  - includes folder
    - ajax.php
  - core folder
  - template folder
index.php

Now on every page I have an init.php required. In ajax.php for eg.

<?php require_once 'core/init.php'; ?>
// rest of the code

The issue I am having is that the init.php file won't run as long as ajax.php is in includes folder. I get an error something like this.

Warning: require_once(core/init.php): failed to open stream: 

If i move the ajax.php back to the root directory, it'll work fine.

On index.php, this is how I am calling the ajax.php

<a href="includes/ajax.php?id="324"></a>

core/init.php works fine when requiring in the "templates" folder.

Perhaps someone can tell me the solution to this?

Was it helpful?

Solution

Try using PHP's chdir function. This will change the working directory that your PHP environment is working in, and therefore the relative paths that your include functions are looking in. Ideally, you'd want to call chdir with the root of your project, so that when you do require_once('core/init.php'); you don't end up calling from the core directory, but rather your . directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top