如何对两个输入字符串进行文件检查?

时间:2018-10-10 06:43:52

标签: php html input match

目前,我正在尝试构建一个非常简单的文件检查代码,该代码需要将两个输入作为文件进行检查:

<?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),但是它将返回一个空白屏幕。

1 个答案:

答案 0 :(得分:1)

在其他情况下,代码中缺少"时出错,您应该使用文件的完整路径而不是文件名

所有问题均已解决

$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}";

有关绝对路径和相对路径的更多信息

  • 如果路径是从系统根目录开始构建的,则称为绝对路径。
  • 如果路径是从当前位置开始构建的,则称为相对路径

Live Demo

有关Reference OR more details about absolute and relative path函数的更多详细信息