Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include only if file exists

Tags:

php

I need an include function / statement that will include a file only if it exists. Is there one in PHP?

You might suggest using @include but there is an issue with that approach - in case the file to be included exists, PHP will not output warnings if the parser find something wrong in the included file.

like image 648
Emanuil Rusev Avatar asked Jan 05 '11 14:01

Emanuil Rusev


People also ask

How do I check to see if a file exists?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .

How do I know if a file is present in Makefile?

just do @rm -f myfile. Because of the "-f" flag, "rm" will exit with 0 regardless of whether the file exists or not. Or, -rm myfile the lead dash telling make to ignore any error.

How do I check if a file exists in Python?

In Python, you can check whether certain files or directories exist using the isfile() and isdir() methods, respectively. However, if you use isfile() to check if a certain directory exists, the method will return False. Likewise, if you use if isdir() to check whether a certain file exists, the method returns False.

How do I check if a file exists in C++?

Use ifile. open(): ifile. open() is mainly used to check if a file exists in the specific directory or not.


1 Answers

if(file_exists('file.php'))     include 'file.php'; 

That should do what you want

like image 183
Colum Avatar answered Sep 28 '22 07:09

Colum