require_once(PHPExcel/Classes/PHPExcel.php): failed to open stream - php

I am trying to include PHPExcel to a Silverstripe 3 site to export excel sheets. Right now I am just trying to test, but I get this error when trying to do it:
[Warning] require_once(/sitename/mysite/AddOns/PHPExcel/Classes/PHPExcel.php): failed to open stream: No such file or directory
Thing is I know this file exists since I copied it over myself and have rechecked the path over and over. So I decided "well check if the file exists" using this code:
if(!file_exists(Director::baseURL().'mysite/AddOns/PHPExcel/Classes/PHPExcel.php')) {
echo 'sdf';exit;
}
The path is correct (that is where it is saved) according to the error, but- file does not exist. I am also requiring the file in the same way, with no luck
require_once Director::baseURL().'mysite/AddOns/PHPExcel/Classes/PHPExcel.php';
I have tried everything-checking file permissions, referencing parent folders using ../../, calling it directly like AddOns/PHPExcel, moving it to this new AddOns folder (first tried placing the PHPExcel classes on the root and discovered that Silverstripe doesn't read it then :) )
I know I am doing something wrong but for the life of me I cannot see what. Please help
Thanks

BASE_PATH is the best way to access the web root folder.
require_once(BASE_PATH . '/AddOns/PHPExcel/Classes/PHPExcel.php');
Also this is only an issue if you are not using composer, to solve this issue in the correct way you should use composer.

You should consider using composer to include the PHPExcel class, this will avoid the need to manually require the class and will help you with dependency management.
composer require phpoffice/phpexcel

As pointed out Director::baseURL() will return the URL rather than the filepath.
Instead require relative to the file web root like so:
require_once(BASE_PATH . '/AddOns/PHPExcel/Classes/PHPExcel.php');
As pointed out by both Dan and Barry in the other answers, it's preferable to use composer for dependency management.

Related

Require_once not working (failed to open stream: No such file or directory)

I have a db mysqli connection inside folder myapp/db/dbconn.php. And I'm working with file myapp/add/addcat.php. But when I put require_once 'db/dbconn.php'; it says:
Warning: require_once(../Gats/db/dbconn.php): failed to open stream:
No such file or directory in C:\Program Files
(x86)\Ampps\www\Gats\afegir\afegirgat.php on line 4
How can I fix this? Dbconn works for any file that isn't inside a folder (like myapp/index.php) but doesn't otherwise. Thanks in advance!
You need to require files using the relative location of them regarding your current script. So, if your folder structure is like
- Gats
- db
- dbconn.php
- add
- addcat.php
Then the proper way to require dbconn would be
require_once('../db/dbconn.php');
However, this won't scale if you have a large filebase to mantain, so I'd advise to keep entrypoints to the bare minimum and, if possible, on the root folder of your project, from which the relative route to any other file is evident.
Even better, you could have a single entrypoint, declare a constant inside it to set the BASE_PATH, and include/require everything knowing beforehand the value of BASE_PATH. (this one would need you to implement some kind of router functionality, or adopt a microframework to do that for you).

How come my code works separately but not when I merge it with my controller?

I wrote this code in a test.php file.
<?php
include ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/autoload.php');
require_once ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/Client.php');
require_once ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/Service/YouTube.php');
?>
If I go to this file like this: localhost/MySite/protected/MyYouTube/test.php it works, but if I copy the same code into my controller that is located under the same folder as test.php, I get this:
include(Google_Service.php): failed to open stream: No such file or
directory
There are no conflicts with the imports. In fact, the controller and view can be empty and still I get the same thing.
Apparently that happens when autoload.php is not actually loaded.
How is it possible that when the code is integrated into the website it throws this error?
This is what the path to my site looks like:
localhost/MySite/index.php/user/view It seems that the way I visit the file matters.
I tried several things. I tried importing the test.php into my view or my controller, and still I get the same error. I tried using Yii::app()->basePath and it gives the same problem.
The paths are correct, I have checked several times. How can I fix this?
The include path is actual server path, not site path, if you use / at the beginning, you are telling it to look in the server root.
If you know the absolute path in the server you can use it like /var/www/MySite or c:\mysiteif you don't know that then you use relative paths.
$_SERVER["DOCUMENT_ROOT"] works well with PHP 5.3, not sure if below check your version. Try removing the '/' before the MySite part, as the variable already does it for you so it might be printing like localhost//MySite, although I'm not sure if that should or shouldn't work. ]
Also the autoload php should be loaded with a require_once function, not with the include. Good luck!

PHP File Include

I have searched the forums already, but none of the solutions have seemed to help me.
Basically I have just installed and created a package using composer. I need to autoload the classes, sounds pretty standard.
I followed all the instructions, and have added this line of code to my script:
require_once 'vendor/autoload.php';
The vendor folder is located in the root folder of my server, here:
/root/vendor/autoload.php
So, I added
:/root
To my PHP ini file so that PHP searches in the root folder when looking for includes. I thought that should work but it's not :(
My PHP ini file now looks like this:
.:/usr/lib/php:/usr/local/lib/php:/root
The error message I am getting is this:
[14-Jul-2014 16:46:29 Europe/London] PHP Fatal error: require_once(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/lib/php:/usr/local/lib/php:/root') in /home/owned/public_html/trythis/ow_plugins/oftokbox/bol/service.php on line 38
Any ideas?
You implicitly state you are using Composer for a project. By doing so you must have a composer.json file somewhere. And Composer will create a vendor folder directly in the folder containing this file.
So if you also have a file index.php in the folder containing the composer.json, to include the autoloader you would use require 'vendor/autoload.php';.
If however you follow some security guidelines and have a dedicated folder containing public files, then the file would for example be called public/index.php, and for this file to reach the autoloader, the relative path would be require '../vendor/autoload.php';.
Composer cannot give a one-instructions-fits-all direction because it depends on which folder structure you have. But including the composer autoloader is just the same task as including any other file with a relative path.

PHP Require Path

I have a directory layout something like /public_html/foo/bar.php that I want required in my file with my file require statement being currently require('../foo/bar.php') but I'm not getting to it (I keep getting an error saying its not a directory? and failed to open stream).
My file that I'm putting the require in is in /public_html/foo2/ so I need to go up one directory listing and then find the file to include it but I'm not sure what I'm doing wrong right now. Any guesses?
Actual code:
require('../objects/user.php/');
Thanks for your help!
You have a / behind your ../objects/user.php suggesting user.php is a directory.
That's why it tells you it is not a directory, because it indeed isn't.
Remove it and you'll probably be fine. (so ../objects/user.php/ becomes ../objects/user.php)

Including file in cakephp

I'm getting mad. I can't understand what's the problem.
Obviously i'm trying a require_once() but it doesn't work. As you can see, the file should be in the right place. Anyone have a clue why it doesn't work?
If you have a file in your vendors folder (i.e., vendors/filename.php)
You can do:
App::import('Vendor', 'filename');
Here's some more examples of including vendors
You can include files in different ways:
http://book.cakephp.org/2.0/en/core-utility-libraries/app.html
In your case you're using Lucene as a vendor, so the correct include would be:
App::import('Vendor', 'lucene/Search/Lucene');
to load /app/Vendor/lucene/Search/Lucene.php
Just place your path appropiately. For instance
require_once('/cake/importedfolder/imported_file.php');
In as much as your .htaccess is set to have permission and mod_rewriting is enabled.
To narrate better: you can place the file or folder containing the file you want to import in the 'webroot' folder
For instance we want to require a connection.php file (just to explain tho).
All you have to do is place the file in 'webroot'
And then require it. : thus 'require_once(/cake/connection.php');
Let me know if this helps.....

Categories