0

I want to use relative root path in my PHP application. I am developing on windows.

- wwww
  -my-api-2
  -my-api-1
     -config
         -configuration.php
     -inf
         -appService.php
     -index.php

appService.php is including configuration.php like this.

<?php
  include_once "/config/Configuration.php";

index.php is including appService.php like this:

<?php
  include_once "inf/appService.php";

This is working on windows, but not workin when uploaded on linux server. Warning is like this:

Warning: include_once(/inf/Configuration.php) [function.include-once]: failed to open stream: No such file or directory in /var/www/my-api-1/inf/appService.php on line 1

Solution is PHP case sensitive for file names. I am new in php so I noticed that developed APPS on windows may not work on linux because of case sensitive

5 Answers 5

2

It looks like you are using an uppercase "C" for the filename in PHP (Configuration.php) while the actual filename is all lowercase (configuration.php). Windows filenames are case-insensitive. On Linux, however, they are not. Change the filename in PHP accordingly.

include_once "/config/configuration.php";
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

include_once "./config/Configuration.php";

and

include_once "./inf/appService.php";

instead. Linux requires the dot for the current path. Make sure to check your filenames, since Linux is case sensitive - e.g. appService.php is not AppService.php.

Comments

1

As someone else mentioned, first of all, you should be aware of your casing. Without getting into too much details about filesystems, uppercased and lowercased filenames are treated independently (two files "Configuration.php" and "configuration.php" can live in the same folder and are different) in Linux, whereas Windows will not allow you to name two files the same name with different casing.

As a rule of thumb, unless you're making PHP classes, name your files in lowercase and refer to them in lowercase, that'll avoid any confusion or issues.

In addition to this, I would recommend you define a "root path" constant in index.php that you can include files from, such as "APP_BASE_PATH" (just a suggestion).

In index.php:

<?php
  // Check to see if an APP_BASE_PATH constant is defined, or define one
  defined('APP_BASE_PATH') || define('APP_BASE_PATH', realpath(__FILE__));

As long as all files are included "from" index.php (that means, you first include files via index.php, and then files include other files), you can include files with include/require/_once using the full relative path, like this:

<?php
  require_once APP_BASE_PATH . '/config/configuration.php';

For added multiplatform compatibility, consider using the DIRECTORY_SEPARATOR constant when including files:

<?php
  require_once APP_BASE_PATH . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'configuration.php';

This constant is fairly long to type out when you have lots of files, so I usually just define a DS constant to help me out:

<?php
  defined('DS') || define('DS', DIRECTORY_SEPARATOR);
  require_once APP_BASE_PATH . DS . 'config' . DS . 'configuration.php';

Comments

1

it's issue of case sensitive file name. You will not get any issue because windows file-names are case insensitive but linux is different and it's case sensitive. So you need to change your file name or call it as it's name.

without changing file name

include_once "./config/Configuration.php";
include_once "inf/appService.php";

or change file name to

configuration.php
   appservice.php

Comments

0

in appService.php use

include_once "../config/Configuration.php";

as the config and inf folders are on the same level.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.