2

I have several separate websites that live in separate directories. For includes that they have in common, I have it living in the root directory where the rest of them live.

user@hostname:/var/www$ ls
website_1 website_2 website_3 common_files 

I want to include a Zend package, so I have my include path

ini_set("include_path", get_include_path() . ":/var/www/common_files/Zend");
require_once("Mail.php");

Mail.php loads okay, but then somewhere in there is this line

require_once 'Zend/Mail/Transport/Abstract.php';

which gives this error

Warning: require_once(Zend/Mail/Transport/Abstract.php): failed to open stream: No such file or directory in var/www/common_files/Zend/Mail.php on line 26

So php doesn't recursively descend into the directory structure of the include paths. Do I have to move Zend into each website direcory, spell out the path to every include, or what?


BTW Abstract does exist:

user@host:/var/www/common_files/Zend$ tree -d 
...
`-- Mail/Transport
    |-- Mail/Transport/Abstract.php
    |-- Mail/Transport/Exception.php
    |-- Mail/Transport/Sendmail.php
    `-- Mail/Transport/Smtp.php

9 directories, 32 files
user151841
  • 17,377
  • 29
  • 109
  • 171
  • Try using the constant `PATH_SEPARATOR` instead of the colon... so `get_include_path() . PATH_SEPARATOR . '/var/www/common_viles/Zend');` – ircmaxell Aug 20 '10 at 16:07
  • This has bugged me too many times. When you include a file, it is run from the same path is the including file is. Unless they are in the same folder, any relative paths in the included file will cause errors. Does anyone know of a solution to this? – Adrian Schmidt Aug 20 '10 at 16:16
  • 2
    @Adrian, I think it is common practice to use dirname for this. `require_once dirname(__FILE__) . '/relative/path/../here';` – Brandon Horsley Aug 20 '10 at 16:33
  • I guess the solution is that library files shouldn't have hard-coded includes in them :P – user151841 Aug 20 '10 at 16:48
  • @Brandon Ah, yes, that would make the path look absolute when compiled, right? Thanks! – Adrian Schmidt Aug 21 '10 at 12:40

4 Answers4

5

EDIT: You want to change your include_path to include /var/www/common_files

What, if anything, is still broken after you do this?

Brandon Horsley
  • 7,956
  • 1
  • 29
  • 28
  • Where do I include it the second time? The `require_once` line you reference is in Zend's Mail.php library file, not in my file. They intended it to work this way. – user151841 Aug 20 '10 at 16:15
  • 1
    If the require_once is outside of your control, then try changing the ini_path to just `/var/www/common_files` and alter your `Mail.php` to be `Zend/Mail.php` – Brandon Horsley Aug 20 '10 at 16:19
  • 1
    Exactly. ZF expects the top-level Zend/ directory to be inside a directory in the include_path. OP wants `/var/www/common_files` in his include path. – timdev Aug 20 '10 at 16:21
1

Stupid answer: Does Abstract.php exist?

require_once 'Mail/Transport/Abstract.php';

Try this, because Mail.php iz already in Zend folder, I guess it looks for /Zend/Zend/.../Abstract.php

Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
  • I would include that, but there are other includes, so is there a way around writing an include for each file? Why can't the `require_once` that the Zend folks wrote into their files just work? – user151841 Aug 20 '10 at 16:23
1

It looks like Zendis the working directory here, so your statement is looking for /Zend/Zend/Mail/Transport/Abstract.php. Try just cutting off Zend from the statement and it should work fine.

Andy
  • 3,132
  • 4
  • 36
  • 68
  • IT works, but I don't get it. If the include_path is just `/var/www/common_files` and I `require_once('Mail.php')`, how does it know to put the `Zend` in the middle? The path to `Mail.php` is `/var/www/common_files/Zend/Mail.php` – user151841 Aug 20 '10 at 16:19
  • 1
    Oh, I get it. It *does* recursively search the directory! :P – user151841 Aug 20 '10 at 16:21
  • If Mail.php exists in the same directory as the php file including it, and `.` is in the include_path, then it will be found without searching through the rest of your include_path. – Brandon Horsley Aug 20 '10 at 16:31
  • It doesn't exist in the same directory. It exists one dir above, in a common directory, so I don't have to include it redundantly in five different projects. – user151841 Aug 23 '10 at 14:29
0

you need to set your include path to the path where the Zend folder is. then you include the files like so

require_once 'Zend/Mail.php'
Raoul Duke
  • 4,241
  • 2
  • 23
  • 18