file_exists not working with localhost URL

无人久伴 提交于 2019-12-02 18:17:15

问题


I have this piece of code in PHP:

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

But file_exists always returns false. The value passed to the function is:

echo $_POST['current_folder']);  //This prints: http://localhost/wordpress/wp-content/music

I also tried with different folders on the localhost. The function always returns false.

I also tried is_dir(). But even this function returns false with the above URL.

There are many related questions on Stack Overflow. But most of them suggest that file_exists only works with relative URLs. But from this link it is clear that http:// URLs are also supported by the file_exists function.

What am I missing?


回答1:


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



回答2:


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).



来源:https://stackoverflow.com/questions/28931386/file-exists-not-working-with-localhost-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!