当文件名中包含变量时,PHP file_exists返回false

时间:2018-07-06 14:59:09

标签: php filenames file-exists

我想检查服务器上是否存在group.id文件。但是,当我检查它时,返回值为false。

jpeg

原始静态文件名返回clearstatcache(); // the $img variable is dynamically got from $split[1] which is something like image.jpeg" /> $img = str_replace('"','',$split[1]); // remove double quotes $img = str_replace('/>','',$img); // remove img end tag $img = str_replace(' ','',$img); // remove spaces $filename = "uploads/image.jpeg"; // original file name $fn = "uploads/".$img; // file name with dynamic variable in it if(file_exists($fn)){ echo "yes"; }else{ echo "no"; } // Check if the two strings are the same and they are if($fn == $filename){ echo "same"; } ,而动态文件名返回yes。我检查并确定服务器上的nosafe_mode,两个变量(off$fn)完全相同。如果我只是简单地使$filename等于$img而没有任何image.jpeg,它也会返回str_replace并回显true
总的来说,我不知道yes变量有什么问题,如果变量相同,为什么它会给我两个不同的结果?

1 个答案:

答案 0 :(得分:1)

您在某处的调试逻辑中存在严重缺陷,请尝试以下方法:

echo '<hr/>';

clearstatcache();

// the $img variable is dynamically got from $split[1] which is something like image.jpeg" />
$img = str_replace('"','',$split[1]); // remove double quotes
$img = str_replace('/>','',$img); // remove img end tag
$img = str_replace(' ','',$img); // remove spaces

$filename = "uploads/image.jpeg"; // original file name
$fn = "uploads/".$img; // file name with dynamic variable in it
if(file_exists($fn)){
    echo '$fn: yes';
    echo '<br/>';
}else{
    echo '$fn: no';
    echo '<br/>';
}

if(file_exists($filename)){
    echo '$filename: yes';
    echo '<br/>';
}else{
    echo '$filename: no';
    echo '<br/>';
}

// Check if the two strings are the same and they are
if($fn == $filename){
    echo "same";
    echo '<br/>';
}
else
{
    echo 'different';
    echo '<br/>';
}

echo '<pre>';
var_dump( $split[1], $filename, $fn )
echo '</pre>';

echo '<hr/>';