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!
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.
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.
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....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With