You are here:  » Image Cache load no_image.jpg

Support Forum



Image Cache load no_image.jpg

Submitted by webie on Sun, 2007-10-28 15:19 in

Hi Dave,

I have tried to modifiy your script to load image from the server if no image found But i can't get it to work you done this for me on preloadCache

cheers dave.

I just having new search script coded for Price tapestry to use the Sphinx full text search engine when the coder is done would mind if i email it to you to check it over I can't wait this does not touch tapresty code it queries sphinx index and passes back to price tapestry very very very fast I must say.

here is the code

<?php
  
function cacheFetch($url,$age)
  {
    
// directory in which to store cached files, must be writable by PHP
    
$cacheDir "cache/";
    
// cache filename constructed from MD5 hash of URL
    
$filename $cacheDir.md5($url);
    
// default to fetch the file
    
$fetch true;
    
// but if the file exists, don't fetch if it is recent enough
    
if (file_exists($filename))
    {
      
$fetch = (filemtime($filename) < (time()-$age));
    }
    
// fetch the file if required
    
if ($fetch)
    {
      
$maxRetry 2;
      do
      {
        
// shell to wget to fetch the file, storing the exit code in $error
        
exec("wget -N -O ".$filename." \"".$url."\"",$output,$error);
        
// update timestamp to now
        
exec("touch ".$filename);
        
// keep trying if wget failed and not reached $maxRetry
       
} while( $error && $maxRetry--);
    }
    
// return the filename only if wget did not fail
    
if (!$error)
    {
      return 
$filename;
    }
    else
    {
      
// as an error occured, copy the default image as the image
      
unlink($filename);
      
copy("no_image.jpg",$filename);
      return 
$filename;
    }
  }
  
$src $_GET["src"];
  
$img cacheFetch($src,604800);
  
readfile($img);
?>

Submitted by support on Sun, 2007-10-28 15:29

Hi,

The most likely reason for the above code not working is that the /cache/ directory is not writable PHP. You can this with the following script:

<?php
  
if (!is_writable("cache/"))
  {
    print 
"Cannot write to cache/ - check permissions";
  }
  else
  {
    print 
"OK!";
  }
?>

If PHP cannot write to the cache directory, the easiest way is to make it "World Writable". You should be able to do this from your FTP program. Right click on the cache directory in your remote window and look for a "Properties..." or "Permisssions..." menu option. Then, look for "Write Access", and check all the boxes alongside User/Group/World. That should do the trick...

Cheers,
David.