Unable to include a php file in typo3

941 views Asked by At

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?

3

There are 3 answers

0
Maunil Prajapati On

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

0
Benni On

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.

0
Jainish On

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