Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php require_once path difference between windows and linux

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!

like image 259
Ivin Avatar asked Sep 07 '12 10:09

Ivin


People also ask

How does require_once work in PHP?

The require_once keyword is used to embed PHP code from another file. If the file is not found, a fatal error is thrown and the program stops. If the file was already included previously, this statement will not include it again.

What is the difference between require and require_ once in php?

The require() function is used to include a PHP file into another irrespective of whether the file is included before or not. The require_once() will first check whether a file is already included or not and if it is already included then it will not include it again.


1 Answers

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....

like image 60
Mathlight Avatar answered Oct 01 '22 23:10

Mathlight