[英]php include file only if existing
我正在測試php中的一些緩存代碼:
if (is_readable($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
[…]
它應該做什么:僅在$ cachefile已經存在時才運行include。 如果不是這樣,應該繼續嘗試而不嘗試包含$ cachefile。
它的作用:每次嘗試加載$ cachefile時,因此會在不存在時創建php警告。
我不知道如何解決它,因為我嘗試了通常會阻止include被執行的所有操作。 有人可以幫忙嗎?
謝謝!
if(file_exists('file.php'))包括'file.php';
這樣嘗試
if (isset($cachefile) && ((time() - $cachetime) < filemtime($cachefile)))
{
include($cachefile);
}
您可以使用isset
或file_exists
函數,這是文檔=>
http://php.net/manual/zh/function.file-exists.php
http://php.net/manual/zh/function.isset.php
注意 :在()中Wrap time() - $cachetime
$cachefile
,還要確保$cachefile
的網址正確
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.