2009-07-02 42 views
0

我无法理解一件事。在代码中,例如:Php - 文件路径的烦恼

$filePath = 'http://wwww.server.com/file.flv'; 

if(file_exist($filePath)) 
{ 
    echo 'yes'; 
} 
else 
{ 
    echo 'no'; 
} 

为什么脚本返回“否”,但是当我将该链接复制到它下载的浏览器时?

回答

2

file_exists()函数正在查找从服务器文件系统的角度存在的文件或目录。如果http://www.server.com/相当于到/ home /用户名/的public_html /那么你需要让你的代码:

$filename = '/home/username/public_html/file.flv'; 
if(file_exists($filename)) 
{ 
//true branch 
} 
else 
{ 
//false brach 
} 

更多信息,请参见http://php.net/file_exists

0

file_exists()检查文件系统文件和目录。使用fopen()也可以查看该网址是否可以访问。如果相应的服务器将为该资源返回404 Not Found,fopen()将返回false并发出警告。更好的解决方案是发出HTTP HEAD请求。

0

首先,您需要使用的php函数是file_exists()和末尾的's'。其次,我认为文件的路径必须是本地文件路径,而不是URL。不知道虽然...

1

使用

$_SERVER["DOCUMENT_ROOT"] 

,以确保正确的文件系统路径,不依赖由开发或生产系统例如。

在这种情况下

,这将是

$filePath = $_SERVER["DOCUMENT_ROOT"].'/file.flv'; 
0

做:

function isExistsFileOnMyWebsite($fileName) { 
    return file_exist($_SERVER['DOCUMENT_ROOT'].'/'.$fileName); 
} 

if(isExistsFileOnMyWebsite('file.flv')) 
{ 
    echo 'yes'; 
} 
else 
{ 
    echo 'no'; 
}