0

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

这是我的代码:

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我在尝试加载图像之前证明了文件!

4

1 回答 1

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
}
于 2013-09-13T07:18:17.873 回答