Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force to include file from same directory and not include_path

Tags:

include

php

I am trying to use FPDF_Code39 class to print bar codes that extends the FPDF class. I am getting this error:

Warning (2): include(helveticab.php) [function.include]: failed to open stream: No such file or directory [D:\xampp\php\PEAR\fpdf.php, line 541]

The catch is in D:\xampp\php\PEAR\fpdf.php. It is including fpdf.php from the PEAR whereas I want it to include fpdf from the same directory as FPDF_Code39 class as I have the latest FPDF there.

I confirmed this issue by temporarily renaming PEAR/fpdf.php and everything works like a charm.

How do I force it to pick up FPDF from the same directory rather than include_path? Here is the require's that I have...

require('./fpdf.php'); \\I read at many places that this will do it.
require(dirname(__FILE__).'/fpdf.php');
require('fpdf.php')

Your help is really appreciated...

like image 795
Gurpreet Singh Avatar asked Jan 24 '13 11:01

Gurpreet Singh


People also ask

What are include () and require () functions?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.

What is dirname (__ file __) in PHP?

dirname(__FILE__) allows you to get an absolute path (and thus avoid an include path search) without relying on the working directory being the directory in which bootstrap. php resides. (Note: since PHP 5.3, you can use __DIR__ in place of dirname(__FILE__) .)

How do I return a file path?

Starting with “/” returns to the root directory and starts there. Starting with “../” moves one directory backwards and starts there. Starting with “../../” moves two directories backwards and starts there (and so on…) To move forward, just start with the first subdirectory and keep moving forward.


1 Answers

The second one is the most stable because it does not depend on the include path (like the third) nor on the current working directory (like the first).

Since PHP 5.3 you can also write

require __DIR__ . '/fpdf.php';

instead

require dirname(__FILE__).'/fpdf.php';
like image 87
Fabian Schmengler Avatar answered Nov 14 '22 23:11

Fabian Schmengler