0

I try to include the PHPExcel IOFaktory.php in a php file by doing this

require_once '/include/phpexcel/Classes/PHPExcel/IOFactory.php';

But when I do I get this error

Warning: require_once(/include/phpexcel/Classes/PHPExcel/IOFactory.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/test/kvitto/index.php on line 10

Why is it looking for the file in the same directoy when I want it to start from root? The PHPExcel library is located in ROOT/include

  • are you sure that is the right folder structure in relation to index.php? – Andy Holmes Jul 21 '14 at 15:46
  • 1
    Check again. `require_once` **is** attempting to look for the file in the root (look at that path - it starts with `/`). Double and triple-check to make sure the file *does* in fact exist. – esqew Jul 21 '14 at 15:47
  • What makes you think it's not looking in the path you specified? – David Jul 21 '14 at 15:47
  • 1
    try `var_dump(getcwd());`. make sure your current directory is what you think it is. – Jeff Lambert Jul 21 '14 at 15:48
  • 1
    Indeed. It's looking exactly where you've told it to look, and the file's not there. Double-check everything, including your spelling and capitalisation. (The error message is telling you both the path to the file it's trying to include—which looks right, according to what you've described, and also the path to the file that's having a problem including it, as it would for any PHP error.) – Matt Gibson Jul 21 '14 at 15:49
  • This is the path if i rightclick the file and select show info in finder: "/Applications/MAMP/htdocs/include/phpexcel/Classes/PHPExcel/IOFactory.php" – user3844170 Jul 22 '14 at 09:47
  • Well, then that's your problem. The path to the file is `/Applications/MAMP/htdocs/include/phpexcel/Classes/PHPExcel/IOFactory.php`, and you're telling it that it's `/include/phpexcel/Classes/PHPExcel/IOFactory.php`. Those things are not the same. I think your understanding of "root" may be wrong. PHP's `include` or `require` statements can range across your entire filesystem, right from the top. The "root"—that is "/"—is the root of your filesystem, i.e. in your case, with MAMP on a Mac, the hard drive that you installed MAMP on. – Matt Gibson Jul 22 '14 at 11:28
  • Wow. I had no idea! I really thought ROOT was considered the WWW/htdocs folder by PHP. Now I get it! – user3844170 Jul 22 '14 at 12:55
  • Cool. I've tried to expand that comment into a clearer answer for you. – Matt Gibson Jul 23 '14 at 07:43

1 Answers1

0

PHP's include and require functions aren't limited to your webroot; they can range across your whole filesystem. Therefore the root, i.e. "/" directory that include paths refer to is generally the root of the disk/volume/whatever that your including PHP file is on.

This is different from the "document root" of your website, which effectively maps a root URL to a particular directory.

So, to include your file using an absolute path from the root, you'd need:

require_once '/Applications/MAMP/htdocs/include/phpexcel/Classes/PHPExcel/IOFactory.php'

Often, people will use relative paths to avoid needing long paths like this, and to make their code easier to move. So if the file you're doing the require_once in is:

/Applications/MAMP/htdocs/test/kvitto/index.php

...then you should find that this works:

require_once '../../include/phpexcel/Classes/PHPExcel/IOFactory.php'

Because stepping up two directories from kvitto, where your index.php is, gets you up to htdocs, and you can then descend from there.

PHP has lots of mechanisms to make this easier. You can use the __DIR__ magic constant ("current directory name") in include paths, or go the whole way and roll your own autoloader for classes if you want.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128