webent Posted August 28, 2008 Share Posted August 28, 2008 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 https://forums.phpfreaks.com/topic/121694-solved-loop-problem/ Share on other sites More sharing options...
sasa Posted August 28, 2008 Share Posted August 28, 2008 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 https://forums.phpfreaks.com/topic/121694-solved-loop-problem/#findComment-627755 Share on other sites More sharing options...
webent Posted August 28, 2008 Author Share Posted August 28, 2008 Wow, very nice... Thank you so very much sasa!!! It worked like a charm. Link to comment https://forums.phpfreaks.com/topic/121694-solved-loop-problem/#findComment-627760 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.