PHP file_exists returns false 当变量在文件名中时

PHP file_exists returns false when variable is in file name

我想检查我的服务器上是否存在 jpeg 文件。但是,当我检查它时,return 值为 false。

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

原始静态文件名return返回yes,而动态文件名返回no。我检查过 safe_mode 在我的服务器上是 off 并且两个变量($fn$filename)完全相同。如果我只是简单地使 $img 等于 image.jpeg 而没有任何 str_replace 它也会返回 true 并回显 yes.
总的来说,我不知道 $img 变量有什么问题,如果变量相同,为什么它会返回两个不同的结果?

你的调试逻辑某处存在严重缺陷,试试这个:

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/>';