简体   繁体   English

php仅在存在时包含文件

[英]php include file only if existing

I'm testing some caching code in php: 我正在测试php中的一些缓存代码:

   if (is_readable($cachefile) && (time() - $cachetime
     < filemtime($cachefile))) 
  {
            include($cachefile);
   […]

What it should do: Run include only if $cachefile already exists. 它应该做什么:仅在$ cachefile已经存在时才运行include。 If not it's supposed to proceed without trying to include $cachefile. 如果不是这样,应该继续尝试而不尝试包含$ cachefile。

What it does: It tries to load $cachefile every time therefore creating a php warning when it does not exist. 它的作用:每次尝试加载$ cachefile时,因此会在不存在时创建php警告。

I have no idea how to fix it since I tried just about everything that would normally prevent include from being excecuted. 我不知道如何解决它,因为我尝试了通常会阻止include被执行的所有操作。 Can someone help? 有人可以帮忙吗?
Thanks! 谢谢!

if(file_exists('file.php'))包括'file.php';

Try like this 这样尝试

if (isset($cachefile) && ((time() - $cachetime) < filemtime($cachefile))) 
{
    include($cachefile);
}

you can use isset or file_exists functions, here is documentations => 您可以使用issetfile_exists函数,这是文档=>

http://php.net/manual/en/function.file-exists.php http://php.net/manual/zh/function.file-exists.php

http://php.net/manual/en/function.isset.php http://php.net/manual/zh/function.isset.php

Note : Wrap time() - $cachetime in () , also make sure $cachefile 's url is right 注意 :在()中Wrap time() - $cachetime $cachefile ,还要确保$cachefile的网址正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM