Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查PHP中是否存在文件_Php - Fatal编程技术网

检查PHP中是否存在文件

检查PHP中是否存在文件,php,Php,我想检查文件是否存在,并显示定义的图像。我尝试使用下面的代码。它总是只显示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

我想检查文件是否存在,并显示定义的图像。我尝试使用下面的代码。它总是只显示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'>";
 } 
$fileUrl=”http://localhost/Report/1111.pdf";
$AgetHeaders=@get\u头($sourcePath);
if(preg|u match(“| 200 |”),$AgetHeaders[0])){
//文件存在
回声“;
}否则{
//文件不存在
回声“;
} 

你用谷歌搜索过吗

文件“”已存在 文件\u存在-检查文件或目录是否存在

bool文件_存在(字符串$filename)

例如:

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

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

使用此功能:-

if (file_exists('filename')) {
 …do something
 }

如果要检查外部文件,可以尝试此功能

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) 

功能很好

为什么我的服务器不工作<代码>你说的“不工作”是什么意思?它给出了一个错误?有什么问题吗?即使文件“1.pdf”存在,代码也只显示“no.png”!没有错误!尝试使用相对路径而不是绝对路径。相对路径有效!我如何使用绝对路径?为什么对我的服务器无效!