查找文件是否存在,如果不存在,则还原为原始路径

时间:2011-02-04 20:28:11

标签: php wordpress file-exists

我写了一个函数来获取图像路径,在它的末尾添加一些文本,然后将它们连接在一起。我正在尝试编写一个if else分支,以便在我将新文本附加到图像后测试文件是否存在。

例如,如果我请求图像路径:

http://example.com/image1.jpg

可以修改为:

http://example.com/image1-150x100.jpg

该函数适用于WordPress,因此需要$ post-> ID参数,自定义字段名称,预期目标的宽度和高度。

这是功能:

function get_galleria_image ( $post_id, $customFieldName, $width, $height ) {

    $imagePath = get_post_meta($post_id, $customFieldName, true);

    $splitImage = explode('.', $imagePath);

    $count = count($splitImage);

    $firstHalf =  array_slice($splitImage, 0, $count-1);

    $secondHalf = array_slice($splitImage, $count-1);

    $secondHalf[0] = '-' . $width . 'x' . $height . '.' . $secondHalf[0];

    $firstHalfJoined = implode('.', $firstHalf);

    $completedString = $firstHalfJoined . $secondHalf[0];

    // if the filename with the joined string does not exist, return the original image
    if (file_exists($completedString)){
        return $completedString;
    } else {
        return $imagePath;
    }
}

我知道这很粗糙,所以欢迎任何强调此代码并使其“更好”的想法。我发现的是该函数总是返回原始图像路径。 file_exists()可以采用像“http://example.com/image1.jpg”这样的绝对路径,还是只接受根样式的绝对路径,比如'/path/to/file/image1.jpg'?< / p>

4 个答案:

答案 0 :(得分:3)

file_exists不接受url路径。尝试使用此网址的路径检查给定的网址是否为图片

function isImage($url)
  {
     $params = array('http' => array(
                  'method' => 'HEAD'
               ));
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) 
        return false;  // Problem with url

    $meta = stream_get_meta_data($fp);
    if ($meta === false)
    {
        fclose($fp);
        return false;  // Problem reading data from url
    }

    $wrapper_data = $meta["wrapper_data"];
    if(is_array($wrapper_data)){
      foreach(array_keys($wrapper_data) as $hh){
          if (substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") // strlen("Content-Type: image") == 19 
          {
            fclose($fp);
            return true;
          }
      }
    }

    fclose($fp);
    return false;
  }

答案 1 :(得分:2)

据我所知,file_exists()仅检查本地文件系统上的文件或目录是否退出。您可能希望通过fsockopen()连接到另一台服务器,然后检查HTTP代码以查明该文件是否存在。

// you will need to set $host and $file

$sock = fsockopen ( $host );
fwrite ( $sock, "HEAD ".$image." HTTP/1.1\r\n" );
fwrite ( $sock, "Host: ".$file."\r\n" );
fwrite ( $sock, "Connection: close\r\n\r\n" );

// Get the first twelve characters of the response - enough to check the response code
if ( fgets ( $sock, 12 ) == "HTTP/1.1 404" )
    //file doesn't exist
else
    //file exists

fclose ( $sock );

答案 2 :(得分:0)

要使程序的第一部分更紧凑,您可以执行以下操作:

$completedString = preg_replace("/(.+)\.([^.]+)$/siU", "$1" . "-" . $width ."x" . $height . "$2", $splitImage);

然后使用@ayush发布的功能检查远程服务器上的文件

答案 3 :(得分:0)

if(@getimagesize($imghttppath))
{
     echo '<img src="'. $imghttppath . '" />';
}
else
{
     echo '<img src="http://www.domain.com/images/default.jpg" />';
}

这有点非常规使用getimagesize,除非你关闭错误,否则它必须是@,因为如果文件不存在则会返回错误。但它就像你能得到的一样简单。使用外部URL,本地URL,本地绝对路径和工作目录的相对路径来检查图像是否存在并且是实际图像。

相关问题