Whats the right way of giving $_SERVER['DOCUMENT_ROOT'] path.


Ie) In my app\design\frontend\base\default\template\folder\index.php i need to call other config files which is in document root of magento.

    // Show only compile error
    error_reporting(E_COMPILE_ERROR );
    // check if pdo library enabled ? 
    require_once($_SERVER['DOCUMENT_ROOT'] . '/magento/test/ajax_table.class.php');
    $obj = new ajax_table();
    $records = $obj->getRecords();
<!DOCTYPE html>


require_once($_SERVER['DOCUMENT_ROOT'] . '/magento/test/config.php'); 
class ajax_table {

If i include the above:

// Show only compile error
error_reporting(E_COMPILE_ERROR );
// check if pdo library enabled ? 
$obj = new ajax_table();
$records = $obj->getRecords();


Also path mismatch here:


ajax = function (params,action){
    type: "POST", 
    url: "ajax.php",    //ajax.php is in var\www\html\magento\test\ajax.php
    data : params+"&action="+action,
    dataType: "json",
    success: function(response){

I get blank screen with controller error...

Table insertion/ other operation is happening if i keep my index.php in same var\www\html\magento\test folder.

Its path issue.

The file seems to get called when i echo inside it, but functionality is not as expected, which works if i keep index.php too in the root folder, but i want it in design folder

  • How you are calling this file in url ` app\design\frontend\base\default\template\folder\index.php `. I think it's wrong folder structure according to magento and magento code not allowed here. – Ashish Jagnani Nov 9 '16 at 8:32
  • where have i specified in url for index.php? im calling index.php from layout – Sushivam Nov 9 '16 at 10:01

I think magento in main folder so you can add easily add it like. try this and let me know

 $includePath = Mage::getBaseDir(). "/test/config.php";
    require_once( $includePath);
  • @SachinS updated the code... – Qaisar Satti Nov 9 '16 at 6:48
  • How about the path in url: in .js file?? – Sushivam Nov 9 '16 at 6:48
  • url: "<?php echo Mage::getBaseDir() ?>ajax.php", if is located with same directory with index.php – Qaisar Satti Nov 9 '16 at 6:49
  • ajax.php is in magento root folder and index.php is in design template folder – Sushivam Nov 9 '16 at 6:51
  • 1
    Ok thanks..working...fixed from this : magento.stackexchange.com/questions/3565/… – Sushivam Nov 9 '16 at 7:52

You can call it like this :

require_once __DIR__ . '/test/ajax_table.class.php';
$path = Mage::getBaseDir('test/ajax_table.class.php');
require_once ($path);

and in ajax_table.class.php use this

$path = Mage::getBaseDir('test/config.php');
require_once ($path); 
  • Invalid dir type requested: test/ajax_table.class.php – Sushivam Nov 9 '16 at 7:02
  • @SachinS Is 'magento' your root directory and all magento files installed in this folder ? – Ashish Jagnani Nov 9 '16 at 7:09
  • yes magento is the root – Sushivam Nov 9 '16 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.