PHP file_exists返回true但createimagefromjpeg运行错误

时间:2013-09-13 07:08:29

标签: php stream file-exists

我有一个非常奇怪的错误!

这是我的代码:

if (file_exists($this->template->large_image_location)) {
    $tmp_src = imagecreatefromjpeg($this->template->large_image_location);  

    //work with $tmp_src
} else {
    //no picture there                      
}

我无法重现错误,但有些用户说他们收到以下错误消息:

  

ErrorException [警告]:
  imagecreatefromjpg(/ srv /.../.../.../ th_vorschau_123.jpg):无法打开流:没有这样的文件或目录!

为什么我甚至会收到此错误?我在试图加载图像前用file_exists证明文件!

1 个答案:

答案 0 :(得分:0)

格拉维奇是对的。但有时 realpath() 会导致问题。所以要利用这个功能。的 [测试]

    <?php
function truepath($path){
    // whether $path is unix or not
    $unipath=strlen($path)==0 || $path{0}!='/';
    // attempts to detect if path is relative in which case, add cwd
    if(strpos($path,':')===false && $unipath)
        $path=getcwd().DIRECTORY_SEPARATOR.$path;
    // resolve path parts (single dot, double dot and double delimiters)
    $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
    $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
    $absolutes = array();
    foreach ($parts as $part) {
        if ('.'  == $part) continue;
        if ('..' == $part) {
            array_pop($absolutes);
        } else {
            $absolutes[] = $part;
        }
    }
    $path=implode(DIRECTORY_SEPARATOR, $absolutes);
    // resolve any symlinks
    if(file_exists($path) && linkinfo($path)>0)$path=readlink($path);
    // put initial separator that could have been lost
    $path=!$unipath ? '/'.$path : $path;
    return $path;
}

if (truepath('redarrowdown.gif')){
    $tmp_src = imagecreatefromjpeg(truepath('redarrowdown.gif'));

    //work with $tmp_src
    echo "success";
}
else
{
    echo "fails";
    //no picture there
}