Unable to include a php file in typo3 - php

I am using Typo3 cms. Under the main folder I have different other folders such as typo3, typo3conf, fileadmin etc...
I have created two php files named myphp.php, and myantoher.php inside the main folder, and I used require() in myphp.php. But when that line is reached during execution, I am receiving the following errors:
Warning: require(doc/PHPMailer/class.PHPMailer.php): failed to open
stream: No such file or directory in
/var/www/domainname.com/doc/contactform.php on line 3 Fatal error:
require(): Failed opening required 'doc/PHPMailer/class.PHPMailer.php'
Why am I receiving an error when I try to require() this file?

It's very easy to add PHP or any other files in TYPO3
You have to script
includeLibs.cookie = fileadmin/cookie/cookie.php
page.20 = USER_INT
page.20 {
userFunc = user_setcookie
}
For more information about TYPO3 stuff you may visit my blog
https://jainishsenjaliya.wordpress.com/2015/04/01/how-to-include-custome-php-file-in-typo3/
Regards,
Jainish Senjaliya

Here you can include your custom PHP file
Using the CONFIG object.
ex:
config.includeLibrary = fileadmin/templates/myscript.php
Using the PAGE object.
ex:
page.100.file = fileadmin/templates/myscript.php

FYI. it's not possible to use includeLibs via TypoScript anymore. Instead, ensure to encapsulate code in PHP functions or PHP classes, and load them via the class loader (by putting them in a simple extension) or via Composer's autoload functionality.
You can still call functions/methods then via PHP all around TYPO3 an call them in Frontend via
page.10 = USER_INT
page.10.userFunc = bennis_function
or if it is a method in a class
page.10.userFunc = Benni\Mack\RandomClassName->my_method
If you want to do it quick and dirty, you can require your file in your AdditionalConfiguration.php file and use it everywhere as well.
Still, best approach is using an extension with PHP classes and PSR-4 autoloading.

Related

Include file with his dependencies in PHP

My root project folder is:
c:\phpproject\data_valid.php
In this folder I have a library I need to use:
c:\phpproject\vendor\egulias\email-validator\src\EmailValidator.php
Inside data_valid.php I include the library I need:
//data_valid.php
include ('vendor\egulias\email-validator\src\EmailValidator.php');
The EmailValidator.php has dependencies:
//EmailValidator.php
namespace Egulias\EmailValidator;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Validation\EmailValidation;
When I run the data_valid.php script I get the error:
Fatal error: Uncaught Error: Class "Egulias\EmailValidator\EmailLexer"
So, the data_valid.php file doesn't know where the EmailLexer.php is, being this file called from EmailValidation.php. If I include EmailLexer.php in data_valid.php it is found
//data_valid.php
include ('vendor\egulias\email-validator\src\EmailValidator.php');
include ('vendor\egulias\email-validator\src\EmailLexer.php');
But more dependendencies are missing:
Fatal error: Uncaught Error: Class "Doctrine\Common\Lexer\AbstractLexer
It seems that PHP doesn't include the dependencies of a library automatically.
How do I do that?
PD. After some research I found that including:
include 'vendor/autoload.php';
It loads everything.
But I am still curious how to do it without using include 'vendor/autoload.php'. For example, I have a bunch of classes inside multiple files in a directory. In this scenario I need to call one class but that class depends in multiple classes defined in multiple files in that directory.
Use composer: https://getcomposer.org/
It also allows for manual inclusion of custom classes etc.
I don't recommend manually including all the necessary classes as you would need some recursive search for this anyway.

Laravel 5 class

I installed laravel 5.1 on my windows 7, after that i tried to open blog/app/Htttp/routes.php.Now there's a error on my browser saying
"Fatal error: Class 'Route' not found in
C:\xampp\htdocs\Laravel_1\blog\app\Http\routes.php on line 14".
How do i remove this error?
Before a class is available in PHP you will need to require/include it. As you have opened up the file directly in the browser, the laravel application is not bootstrapped and none of the dependencies are loaded to be used.
If you open up public/index.php, you will find a line that says
require __DIR__.'/../bootstrap/autoload.php';
// this later calls composer autoload which compiles a file
// that includes all the classes that you have specified in your `composer.json`.
Now that you have opened the file directly, non of the classes are included and hence you get the error.
I am not sure about your intentions for opening that file directly in the browser. I can only suggest you that the error is expected and you should go through the public/index.php for the application to work properly.

Concrete5 - library not being loaded

I'm trying to load a custom library in Concrete5.
I've created a library file, internallib.php, and placed the file in the /libraries folder. This file does not extend any core library files. I keep getting this error:
Warning: require_once(/var/www/concrete/libraries/internalauth.php) [function.require-once]: failed to open stream: No such file or directory in /var/www/portal/concrete/core/libraries/loader.php on line 30
Though if I move the file into the folder /concrete/libraries, I don't get this error. Should I be putting my library file into the concrete/libraries, or /libraries folder? I assumed that since this is a 3rd party library, it should go into the root libraries folder, as not to conflict with any future core updates.
I'm currently running v.5.6.0.2
Any ideas?
You are right, the root libraries folder is the place to go.
You could try to put it in libraries/3rdparty which is where 3rd party libraries should be.
The problem however might come from the way you are calling the library, not from the library itself.
Maybe you could tell us more?

how to require_once in codeigniter

I am trying to extend a library in codeigniter. The only way to do so seems to include the original library using require_once then load the extended library using $this->load->library()
right now I have tried
require_once('ion_auth.php');
require_once('home/SITE_NAME/public_html/FOLDER_NAME/application/libraries/ion_auth.php')
require_once('/home/SITE_NAME/public_html/FOLDER_NAME/application/libraries/ion_auth.php')
but unfortunately not luck..... I keep getting this error
Message: require_once(...) [function.require-once]: failed to open stream: No such file or directory
Weird thing is though this works on my local xampp environment but not on the actual server.
Use CodeIgniter's built in constant, APPPATH
require_once(APPPATH.'libraries/ion_auth.php');
If the library is a codeigniter specific library, as sbaaaang points out, you should use:
$this->load->library('ion_auth');
However, if the library is just a generic PHP class or set of functions, the codeiginter loader may not recognize it. Then you will need to use either one of the generic loading operators (include, include_once, require, require_once), with the APPPATH constant, as also pointed out:
require_once(APPPATH.'libraries/ion_auth.php');
Do you know that Codeigniter has a loader class?
change this
require_once('/home/SITE_NAME/public_html/FOLDER_NAME/application/libraries/ion_auth.php')
to
$this->load->library('ion_auth');
and be sure your libraries/ion_auth.php file it's a class named `class ion_auth{}`

Magento trying to autoload class unnecessarily

I'm using a 3rd party php library to connect to Microsoft Dynamics CRM, which I've placed in the /lib folder of Magento.
I've created a controller for testing purposes, in which I'm including the files from the lib folder (which are being included, as changing the paths throws an error), but when I try and initiate the class defined in one of the included files, i get this warning:
Warning: include(DynamicsCRM2011\Connector.php) [function.include]: failed to open stream: No such file or directory.
It shouldn't be trying to include anything, the class is already defined in a previously included file!
Any ideas how I can prevent this from happening? I'd have thought it would only try to autoload the file if the class is undefined surely?

Categories