Link to home
Start Free TrialLog in
Avatar of pda4me
pda4me

asked on

PHP, optimize code snippet using another call?

I am currently using the following code snip to check to see if a file exists in a large image directory.  The fact that I am using a URL instead of a direct path on the server I think is slowing it down...is it possible to change the file_get_contents to another call and reference the file system path instead?  If so, how do I do this?

<?
$url = "http://www.mysite.com/feeds/sancap/rets_images/";
for($c=1;$c<30;$c++){
        
        if($c<10){
                $c_ext = "0".$c;
        }else{
                $c_ext = $c;
        }
        
        if (file_get_contents($url."{$MLNumber}_{$c_ext}.jpg")){
                $image_array[$c_ext] = "{$MLNumber}_{$c_ext}.jpg";      
        }
        
}

krsort($image_array);

$image_array[] = "{$MLNumber}.jpg"; 

$img_cnt = 1;
$image = "<br>";
foreach($image_array as $key => $value){
        
        $image .= "<a href=/feeds/sancap/rets_images/".$value." rel=\"enlargeimage\" rev=\"targetdiv:loadarea\">";
        $image .= "<img src=/feeds/sancap/rets_images/".$value." alt='' width='100' height='75' border='0' />";
        $image .= "</a>&nbsp;";
        
$img_cnt++;
}

?> 

Open in new window

Avatar of technomoose
technomoose

Hi!

Try this:

Remember that file path is relative to where script is.

Use "is_file" instead of "file_get_contents" to speed things up even more.

<?
$url = 'feeds/sancap/rets_images/';
for($c=1;$c<30;$c++){
        
        if($c<10){
                $c_ext = "0".$c;
        }else{
                $c_ext = $c;
        }
        
        if (is_file($url."{$MLNumber}_{$c_ext}.jpg")){
                $image_array[$c_ext] = "{$MLNumber}_{$c_ext}.jpg";      
        }
        
}

Open in new window

Avatar of pda4me

ASKER

close, but it seems to only show the thumbnail $MLNumber.jpg and no other?  it seems to be missing the other thumbnails?
Ok, just so i understand you correctly...

Is your original code working, but this is not?

<?
$url = 'feeds/sancap/rets_images/';
for($c=1;$c<30;$c++){
        
        if($c<10){
                $c_ext = "0".$c;
        }else{
                $c_ext = $c;
        }
        
        if (is_file($url."{$MLNumber}_{$c_ext}.jpg")){
                $image_array[$c_ext] = "{$MLNumber}_{$c_ext}.jpg";      
        }
        
}


krsort($image_array);

$image_array[] = "{$MLNumber}.jpg"; 

$img_cnt = 1;
$image = "<br>";
foreach($image_array as $key => $value){
        
        $image .= "<a href=/feeds/sancap/rets_images/".$value." rel=\"enlargeimage\" rev=\"targetdiv:loadarea\">";
        $image .= "<img src=/feeds/sancap/rets_images/".$value." alt='' width='100' height='75' border='0' />";
        $image .= "</a>&nbsp;";
        
$img_cnt++;
}

?> 

Open in new window



Avatar of pda4me

ASKER

Yes, original code is working fine.  
ASKER CERTIFIED SOLUTION
Avatar of technomoose
technomoose

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial