繁体   English   中英

PHP file_exists if / then语句

[英]PHP file_exists if/then statement

这是我第一次使用PHP,并且在编写基本的if / then语句时遇到了麻烦。 我想做类似的事情

如果文件存在,则显示html代码,否则显示不同的html代码。

这是我目前所在的位置

<?php
if ( file_exists('pdf/'.'htmlspecialchars($_POST['apt'], ENT_COMPAT)'.'.pdf') {
    echo "the file exists";
}  else {
    echo "file does not exist";
}
?>

我认为这里的问题是我如何写

file_exists('pdf/'.'htmlspecialchars($_POST['apt'], ENT_COMPAT)'.'.pdf')

思想大加赞赏!

尝试:

if (file_exists('pdf/' . htmlspecialchars($_POST['apt'], ENT_COMPAT) . '.pdf')) {
    echo "the file exists";
} else {
    echo "file does not exist";
}

在调用htmlspecialchars()之前,您已经使用了一些引号。 调用函数时,不需要引号。 另请参阅,您没有关闭方括号。

您有引号的问题:

file_exists('pdf/'.htmlspecialchars($_POST['apt'], ENT_COMPAT).'.pdf')

有两个问题。 首先,在if条件的末尾缺少右括号,其次,在对htmlspecialchars的调用周围使用了不应包含的引号。

正确的代码是:

if (file_exists('pdf/'.htmlspecialchars($_POST['apt'], ENT_COMPAT).'.pdf')) {
    echo "the file exists";
}  else {
    echo "file does not exist";
}

您不需要htmlspecialchars。 它将不正确的字符转换为更多不正确的字符。 例如'变成&#0 3 9; (无空格)

暂无
暂无

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

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