file_exists not working with localhost URL

后端 未结 2 617
清酒与你
清酒与你 2021-01-28 11:15

I have this piece of code in PHP:

if (file_exists($_POST[\'current_folder\'])) {
    //do something
} 

But file_exists always retu

相关标签:
2条回答
  • 2021-01-28 11:29

    Use directory path; not web URL:

    <?php
    $filename = '/path/to/foo.txt';
    
    if (file_exists($filename)) {
       echo "The file $filename exists";
    } else {
       echo "The file $filename does not exist";
    }
    ?>
    
    0 讨论(0)
  • 2021-01-28 11:50

    Tested under windows using Apache 2.4.9.

    <?PHP
    $crl = curl_init("http://localhost/symfony2/");
    curl_setopt($crl, CURLOPT_NOBODY, true);
    curl_exec($crl);
    
    $ret = curl_getinfo($crl, CURLINFO_HTTP_CODE);
    curl_close($crl);
    
    if ($ret == 200)
        echo 'File exists';
    else
        echo 'File does not exist';
    ?>
    

    It works, just a note, it requires trailing slash for some reason.

    Code 200 means OK (success).

    0 讨论(0)
提交回复
热议问题