2015-03-08 39 views
0

我有这样一段代码在PHP中:file_exists无法与本地主机URL工作

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

file_exists始终返回false。传递给该函数的值是:

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

我也尝试了localhost上的不同文件夹。该函数总是返回false。我也试过is_dir()。但即使这个函数返回false与上面的URL。

堆栈溢出有很多相关的问题。但大多数人认为file_exists只适用于相对网址。但从this link很明显,http:// URL也支持file_exists函数。

我错过了什么?

+0

使用本地文件路径,而不是hte web路径 – 2015-03-08 20:18:58

回答

0

使用目录路径;不是网址:

<?php 
$filename = '/path/to/foo.txt'; 

if (file_exists($filename)) { 
    echo "The file $filename exists"; 
} else { 
    echo "The file $filename does not exist"; 
} 
?> 
+0

好的谢谢你的答案。有没有办法检查一个远程目录是否存在或不在PHP中? – 2015-03-08 20:27:06

+0

http://stackoverflow.com/questions/5425891/how-do-i-check-with-php-if-directory-exists – 2015-03-08 20:30:12

+0

我试过is_dir()也。但即使它不与http url – 2015-03-08 20:32:28

1

在windows下使用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'; 
?> 

它的工作原理,只是一张纸条,但出于某些原因需要斜线。

代码200表示OK(成功)。