Jump to content

[SOLVED] Loop problem


webent

Recommended Posts

Hi, I have a database that is populated with different vendors products, those vendors provide a link to their product images on their website, which due to high request from my clients, they want those images hosted locally, so I devised a way to do this... Here's my code...

 

function copyFile($image_path,$image,$dirname){
    $url = $image_path . $image;  
    @$file = fopen ($url, "rb"); 
    if (!$file) { 
        echo"<font color=red>Failed to copy $url!</font><br>"; 
        return false; 
    }else { 
        $filename = basename($url); 
        $fc = fopen($dirname."$filename", "wb"); 
        while (!feof ($file)) { 
           $line = fread ($file, 1028); 
           fwrite($fc,$line); 
        } 
        fclose($fc); 
        echo "<font color=blue>File $url saved to PC!</font><br>";
        mysql_query("UPDATE products SET product_image_path = '' WHERE product_image_path = '$image_path' AND product_image = '$image'"); 
        return true; 
    } 
}

$result = mysql_query("SELECT DISTINCT product_image_path, product_image FROM products WHERE product_image_path != ''");
    while ($row = mysql_fetch_assoc($result)) {                       
        copyFile($row['product_image_path'], $row['product_image'], "images/"); 
    }

 

It works fine and dandy, but in alot of cases, multiple products will use the same images, possibly even  hundreds of times... which causes alot of extra work on the part of this script... So what I thought was that I would have it check to see if that image already exists in the directory in which the images are copied to, prior to running the copyFile function, but that's not working, it just keeps copying the same file over and over and over again. Here's my modified code...

 

$result = mysql_query("SELECT DISTINCT product_image_path, product_image FROM products WHERE product_vendor != 'DropShipDirect' AND product_image_path != ''");
    while ($row = mysql_fetch_assoc($result)) {                       
        $dir = '/home/webzcart/public_html/resource/images/';   
        foreach(glob($dir . '*.jpg') as $file){
        $filename = basename($file); 
        if ($row['product_image'] != $filename) {    
        copyFile($row['product_image_path'], $row['product_image'], "images/"); 
        }    
        }
    }

 

Can anyone help me figure out what I'm doing wrong here?

Link to comment
Share on other sites

try

<?php
$dir = '/home/webzcart/public_html/resource/images/';
foreach(glob($dir . '*.jpg') as $file) $filenames[] = basename($file); 

$result = mysql_query("SELECT DISTINCT product_image_path, product_image FROM products WHERE product_vendor != 'DropShipDirect' AND product_image_path != ''");
while ($row = mysql_fetch_assoc($result)) {                       
if (!in_array($row['product_image'], $filenames)) {    
    	copyFile($row['product_image_path'], $row['product_image'], "images/"); 
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.