Including file in cakephp - php

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

Related

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

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.

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 - Going back directories

I've encountered something when trying to include/require a php script that's 2 directories back. It's not really a problem as I figured out a work around, however I'd love an explanation for what's happening.
Here's the file structure:
appCode
db.php (File I'm trying to include)
studentManagement
index.php
dep
getData.php (File I'm trying to include db.php into)
I want to include appCode/db.php in studentManagement/dep/getData.php.
getdata.php is executed with ajax from index.php
When I use:
require_once("../../appCode/db.php");
It doesn't work.
The only way it works is it I change directory first:
chdir("../");
require_once("../appCode/db.php");
Why won't the first method work? I've also tried using include instead of require but it's the same. I'm testing it on mamp 3.0.4.
Any help appreciated!!
that is because when you require(),include() and their variants it's always relative to the initial php file called (in your case index.php)
in fact chdir has nothing to do with it, and this:
require_once("../appCode/db.php");
is the right way to call it.
always place your mental self (!) as if you were index.php when you require files and work with directories. your "active directory" is the one where index.php is placed. you can always verify this current working directory with getcwd() http://php.net/manual/en/function.getcwd.php
If you know your entire directory all the way from root you can use:
http://php.net/manual/en/function.set-include-path.php
Now instead of using relative paths you can always include or require files starting at your include path.
You could put this in getData.php:
set_include_path ('/homepages/www/2/yourworkingpath');//Use your own directory
require_once 'appCode/db.php';
You could also do the same thing in your other files if you need to include and it will always use your include path so you don't have to keep figuring out which directory to change to. Hopefully this helps a bit.

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)

How to include file from another directory

In the root (www) I have two folders.
In the first folder, "folder1", I put a file called register.php.
In the next folder, "folder2", I put files called header.php and footer.php.
I need to include the header and footer files from folder2 in the register.php file.
How can i do this? I tried to use this include ../folder2/header.php
..but it does not work
On some configurations, adding ./ (current dir) does the trick like this:
include './../folder2/header.php';
Alternatively, you can specify in terms of document root:
include $_SERVER['DOCUMENT_ROOT'] . 'folder2/header.php';
<?php include( $_SERVER['DOCUMENT_ROOT'] . 'folder2/header.php' ); ?>
include $_SERVER['DOCUMENT_ROOT'] . '/folder2/header.php';
would work from any directory of the site
it is called absolute path and it's the only reliable way to address a file
However, in real it should be something like
include $_SERVER['DOCUMENT_ROOT'] . '/cfg.php';
// some code
include $TPL_HEADER;
using a variable, previously defined in cfg.php
However, it may fail too. Because you can be just wrong about these paths
And here goes your main problem:
but it does not work
There is no such thing as "it does not work"
There is always a comprehensive error message that tells you what exactly doesn't work and what it does instead. You didn't read it yourself, and you didn't post it here to let us show you a correct path out of these error messages.
include files should generally be kept outside of the server root.
lets say your setup is;
www/website1
and
www/includes
Then you php.ini file, or .htaccess file should stipulate that
include_path=www/includes
then from any of your files, in any directory, no matter how far down the trees they go you simply do:
include 'myfile.php';
where myfile.php is at www/includes/myfile.php
Then you can stop worrying about these issues
include dirname(__FILE__).'/../folder2/header.php';
Try This it is work in my case
<?php require_once __DIR__."/../filename.php";?>
As the PHP manual states here $_SERVER['DOCUMENT_ROOT'] is "The document root directory under which the current script is executing, as defined in the server's configuration file." For this example, $_SERVER['DOCUMENT_ROOT'] will work just fine but. . . By using the new "magic constants" provided in >= PHP 5.3, we can make this code a little safer.
Put your includes in a subfolder, and use the magic constant DIR to make a reference to the included files. DIR returns the directory of the currently executing php file. By using this, you can move your folder containing all your includes anywhere you like in your directory structure, and not need to worry if your includes will still work.

Categories