0
votes

I want to include a php-file in my template. But sometimes the php file is not exist. I try to check it by function "file_exists", but it doesn't work.

{if file_exists("`$plugin`/button.php")}
    {include_php "`$plugin`/button.php"}
{/if}

Fatal error: Uncaught --> Smarty Compiler: Syntax error in template "file:/home/user/www/site/template/template.tpl" on line 456 "{include_php "$plugin/button.php"}" {include_php} file '/button.php' is not readable <-- thrown in /home/user/www/site/smarty/sysplugins/smarty_internal_templatecompilerbase.php on line 456

Is there any way to check if a php file exists?

1
What is this: `$plugin`/button.php is that really the name of the filepath? - Andreas
@Andreas I use folder name in $plugin variable, it works ok in file path - MrSmile
it works ok in file path but in the question you write but it doesn't work.. Which is it? - Andreas
@Andreas it works well if the file exists :) But I want to check if the file exists, because there is fatal error if the file does not exist - MrSmile
Sure it's not the $plugin variable that is empty or not set that creates the error? - Andreas

1 Answers

0
votes

Always check your variables before trying to use them.

In this case it was the $plugin variable that is empty that makes the string concatenation fail.

If you first make sure your variables are set, then if that is true check if the file exist it should work as expected.

{if isset($plugin) && file_exists("`$plugin`/button.php")}
    {include_php "$plugin/button.php"}
{/if}

In pure PHP this should work since if the isset fails it will not even try the file_exist.
Not sure about smarty though.
If it does not work this way you need to make it a separate if.