Php 使用is_文件重复检查if语句

Php 使用is_文件重复检查if语句,php,Php,这看起来有效吗?我仍在为它编写数据库编码,但我想确保我在正确的路径上,这样在测试时就不会有太多错误 $filechk1 = "../temp/files/" . $data[0] . ".doc"; $filechk2 = "../temp/files/" . $data[1] . ".doc"; if(is_file($filechk1) && is_file($filechk2)) { $rec_type = "3"; } else if (!is_

这看起来有效吗?我仍在为它编写数据库编码,但我想确保我在正确的路径上,这样在测试时就不会有太多错误

$filechk1 = "../temp/files/" . $data[0] . ".doc";
$filechk2 = "../temp/files/" . $data[1] . ".doc";

if(is_file($filechk1) && is_file($filechk2)) {
        $rec_type = "3";
    } else if (!is_file($filechk1) && is_file($filechk2)) {
        $rec_type = "2";
    } else if (is_file($filechk1) && !is_file($filechk2)) {
        $rec_type = "1";
    }
看起来您忘了检查这两个文件是否有效。

请简化它

$filechk1 = "../temp/files/" . $data[0] . ".doc";
$filechk2 = "../temp/files/" . $data[1] . ".doc";

$rec_type = 0;
if(is_file($filechk1))
    $rec_type++; // $rec_type += 2;
if(is_file($filechk2))
    $rec_type += 2; // $rec_type++;


此外,如果没有文件,
$rec_type
将为0(对于两个示例)。

只需再次使用类型转换

$rec_type = is_file('../temp/files/'. $data[0] .'.doc') * 1 // just to force an int
          + is_file('../temp/files/'. $data[1] .'.doc') * 2;

两个代码段都给出了错误的结果。第二个甚至根本没有检查第二个文件。@rik:第一个代码段完全按照预期工作。关于第二个,你是对的,经过相应的编辑。海报需要!file1&&file2=>2,file1&!file2=>1。你的代码可以!file1&&file2=>1,file1&!file2=>2。@rik,哦,你明白了,不是吗?我猜OP也知道如何更改值。。。但是谢谢!我认为您的代码比大多数方案更容易理解,正如@ajreal所指出的,您只是缺少了一个检查。
$filechk1 = "../temp/files/" . $data[0] . ".doc";
$filechk2 = "../temp/files/" . $data[1] . ".doc";

$rec_type = 0;
$rec_type += is_file($filechk1) ? 1 : 0;
$rec_type += is_file($filechk2) ? 2 : 0;
$rec_type = 0;
if (is_file('../temp/files/'. $data[0] .'.doc'))
    $rec_type++;
if (is_file('../temp/files/'. $data[1] .'.doc'))
    $rec_type += 2;
$rec_type = is_file('../temp/files/'. $data[0] .'.doc') * 1 // just to force an int
          + is_file('../temp/files/'. $data[1] .'.doc') * 2;