-1

我想检查jpeg我的服务器上是否存在文件。但是,当我检查它时,返回值为 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";
}

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

4

1 回答 1

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/>';
于 2018-07-06T15:14:58.967 回答