【发布时间】:2010-11-07 17:43:29
【问题描述】:
我无法理解一件事。以代码为例:
$filePath = 'http://wwww.server.com/file.flv';
if( file_exist($filePath) )
{
echo 'yes';
}
else
{
echo 'no';
}
为什么脚本返回“否”,但是当我将该链接复制到它下载的浏览器时?
【问题讨论】:
我无法理解一件事。以代码为例:
$filePath = 'http://wwww.server.com/file.flv';
if( file_exist($filePath) )
{
echo 'yes';
}
else
{
echo 'no';
}
为什么脚本返回“否”,但是当我将该链接复制到它下载的浏览器时?
【问题讨论】:
file_exists() 函数从服务器文件系统的角度寻找存在的文件或目录。如果http://www.server.com/ 等于 /home/username/public_html/ 那么您需要编写代码:
$filename = '/home/username/public_html/file.flv';
if(file_exists($filename))
{
//true branch
}
else
{
//false brach
}
请参阅http://php.net/file_exists 了解更多信息。
【讨论】:
使用
$_SERVER["DOCUMENT_ROOT"]
确保正确的文件系统路径,例如不依赖于开发或生产系统。
在这种情况下,它将是
$filePath = $_SERVER["DOCUMENT_ROOT"].'/file.flv';
【讨论】:
file_exists() 检查文件系统文件和目录。也使用 fopen() 查看该 Web URL 是否可访问。如果相应的服务器将为该资源返回 404 Not Found,fopen() 将返回 false 并发出警告。更好的解决方案是发出 HTTP HEAD 请求。
【讨论】:
首先,您需要使用的 php 函数是 file_exists(),末尾带有 's'。其次,我认为文件的路径需要是本地文件路径,而不是 URL。不过不确定...
【讨论】:
做:
function isExistsFileOnMyWebsite($fileName) {
return file_exist($_SERVER['DOCUMENT_ROOT'].'/'.$fileName);
}
if( isExistsFileOnMyWebsite('file.flv') )
{
echo 'yes';
}
else
{
echo 'no';
}
【讨论】: