php require_once path difference between windows and linux

phoenix picture phoenix · Sep 7, 2012 · Viewed 13.1k times · Source

I have a file located in my CakePHP root folder placed under a folder named cron. Path is:

c:/wamp/www/project/cron/daily.php

This file requires another file placed inside vendor folder of cake structure, like this:

require("/../vendors/phpMailer/class.phpmailer.php");

And i run this daily.php from task scheduler. This is the scenario in my development site.(Windows system). It works fine as expected. When i migrated the project to Ubuntu(production site), the require statement started causing issues; it cant find the required file. I made a small change there, like this:

require("../vendors/phpMailer/class.phpmailer.php"); <= removed the preceding slash

And it worked. So my doubt is, is there a difference in how parent directory notation work in widows and Linux? If so, how can i overcome this? Its not feasible to remove a slash every time I move the project from my development site(windows) to production site (Linux).


I tried this:

 require("./../vendors/phpMailer/class.phpmailer.php");

It worked in linux. But gave "no such file directory" error in windows. It seems windows works only with:

 require("/../vendors/phpMailer/class.phpmailer.php");

Solution

From @TWCrap's help problem was solved as follows:

require(dirname(__FILE__)."/../vendors/phpMailer/class.phpmailer.php");

It works in both windows and linux(* tears of joy *). But in windows it produces path as:

C:\wamp\www\project\cron/../vendors/phpMailer/class.phpmailer.php

This path looks ugly and i hope it wont cause probs in future!

-Thanks guys!

Answer

Mathlight picture Mathlight · Sep 7, 2012

AS i remember, when you put 1 dot infront of the line, you start at the directory you are. So then the line must look like this:

require("./../vendors/phpMailer/class.phpmailer.php");

And that should work at windows and linux....