代码之家  ›  专栏  ›  技术社区  ›  squancy

当变量在文件名中时,php file_exists返回false

  •  -1
  • squancy  · 技术社区  · 8 年前

    我想检查一下 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 ,而动态的则会回馈 no . 我查过了 safe_mode off 在我的服务器和两个变量上( $fn $filename )完全一样。如果我只是简单地 $img 等于 image.jpeg 没有任何 str_replace 它也会回报 true 回音荡漾 .
    总的来说,我不知道 美元IMG 变量,如果变量相同,为什么它会返回两个不同的结果?

    1 回复  |  直到 8 年前
        1
  •  1
  •   MonkeyZeus    8 年前

    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/>';
    
    推荐文章