我想检查文件是否存在并显示已定义的图像。我尝试使用如下代码。它总是显示no.png甚至只有1111.pdf存在!
$fileUrl = "http://localhost/Report/1111.pdf";
$AgetHeaders = @get_headers($sourcePath);
if (preg_match("|200|", $AgetHeaders[0])) {
// file exists
echo "<img src='http://localhost/Report/yes.png'>";
} else {
// file doesn't exists
echo "<img src='http://localhost/Report/no.png'>";
}
答案 0 :(得分:1)
你尝试使用谷歌搜索吗?
file_exists
file_exists - 检查文件或目录是否存在
bool file_exists(string $ filename)
示例:
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
答案 1 :(得分:0)
使用此功能: -
if (file_exists('filename')) {
…do something
}
答案 2 :(得分:0)
如果要检查外部文件
,可以尝试此功能function curl_check_file_exist($url)
{
$rarr = array();
$useragent = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); //set our user agent
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$result = substr($result, $header_size);
$ok = $info['http_code'];
if($ok == 200)
{
return true;
}
else
{
return false;
}
}
像这样
if(curl_check_file_exist($url))
{
.....
}
else
{
.....
}
但我想检查本地文件
file_exists(path)
功能很好