目前,我正在尝试构建一个非常简单的文件检查代码,需要将两个输入作为文件进行检查:
<?php
if (file_exists($_POST['input1'] . '.html')) &&(file_exists($_POST['input2'] . '.txt')) {
header("Location: http://www.example.com/exists");
} else {
header("Location: http://www.example.com/noexist);
}
?>
代码应在文件夹中搜索两个文件,名称分别为输入的“input1”和“input2”(1 是 html 文件,2 是 txt),但返回空白屏幕。
最佳答案
您的代码在 else 条件下缺少 "
存在一些错误,您应该使用文件的完整路径而不是文件名
所有问题都已解决
$path= '/your_file_path/';
if (file_exists($path.$_POST['input1'] . '.html') && file_exists($path.$_POST['input2'] . '.txt'))
{
header("Location: http://www.example.com/exists");
}
else
{
header("Location: http://www.example.com/noexist");
}
更好的使用方法
$path1='/your_file_path/'.$_POST['input1'] . '.html';
$path2='/your_file_path/'.$_POST['input2'] . '.txt';
if (file_exists($path1) && file_exists($path2))
使用绝对路径的更多细节
$file_name=$_POST['input1'] . '.html';
$path= dirname(__FILE__) . DIRECTORY_SEPARATOR . "{$file_name}";
关于绝对路径和相对路径的更多信息
- 如果路径是从系统根开始构建的,则称为绝对路径。
- 如果路径是从当前位置开始构建的,则称为相对路径
Reference OR more details about absolute and relative path
有关file_exists()的更多详细信息功能
关于php - 如何对两个输入字符串进行文件检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52734157/