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

标签 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),但返回空白屏幕。

最佳答案

您的代码在 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/

相关文章:

asp.net - html5 输入类型日期时间值未显示在 ASP.NET 网络表单上

C scanf() 问题?

php - qcachegrind错误: Unknown file format.从php xdebug生成的文件

html - 列表标题的 aria 标签,语义正确吗?

PHP:URL 友好字符串

c# - 在密码字段中显示星号而不是无值

python - 如何理解 Yahoo! 的原始 HTML使用 Python 检索数据时的财务状况?

java - hadoop映射器读取多行

php - 更改 Wordpress 中的子菜单换行

php - 我如何在多个表中进行搜索?