我想检查smarty
上的服务器上是否存在webp映像。CCD_ 2是这样的绝对CCD_ 3。
我试过
{if file_exists("https://example.com/image.webp")}
{/if}
和
{if 'https://example.com/image.webp'}
{/if}
但没有一个是有效的。有人能告诉我怎么做吗?
在控制器中,您应该执行以下操作:
if (file_exists($filename))
您也可以像现在尝试的那样在视图中执行此操作,但建议将您的逻辑与视图分离。
不要忘记向$filename
变量添加绝对路径。
编辑:
在smarty的情况下,您可以在php脚本中执行以下操作:
$smarty = new Smarty();
$fileExist = false;
if (file_exists($filename)) {
$fileExists = true;
}
$smarty->assign('fileExists', $fileExists);
$smarty->display('index.tpl');
在视图/模板中,您可以简单地执行以下操作:
{if $fileExists}
在smarty中,不能在URL上使用file_exists()
,但可以在本地文件上使用
我认为,最好的方法是检查你的控制器。
第一种方法:
在您的控制器中,您可以这样写(仅限路径(:https://www.php.net/manual/fr/function.file-exists.php
$filename = '/example.com/image.webp'
if(!file_exist($filename))
$filename = '/example.com/other_image.webp'
然后返回变量$filename
其他方式:
如果你想从URL检查图像是否存在,那么你应该使用这个:
$file = 'http://www.example.com/somefile.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
则//do something...
您必须检查文件INfile_exists("var/www/html/example.com/image.web"(它会注意到Check Http或Https