laanes Posted April 13, 2011 Share Posted April 13, 2011 I wrote a piece of code that lists images that don't have a match in the database: $dir_path = '../images/productImages'; $images = scandir($dir_path); $query = "SELECT image FROM Database_table ORDER BY name DESC"; $results = mysql_query($query); $output = "<h1>Missing Images</h1>"; $output .= "<div class=\"missingImages\">"; $output .= "<p>The following data was found in the Database_table.images field but not in /images/productImages:</p>"; $output .= "<div class=\"missingImg\">"; $output .= "<p class=\"fieldName\">Database_table.images:</p>"; while ($fetch = mysql_fetch_array($results)) { $clean_fetch = str_replace("productImages/", "", $fetch); $diff = array_diff($clean_fetch, $images); foreach (array_unique($diff) as $key => $value) { $output .= $value . "<br />"; } } $output .= "</div>"; $output .= "</div>"; echo $output; Works. Now I would like to check whether there are any files in the folder that are not in the database table. I thought it would be as easy as swapping the array_diff parameters around but it's not. When i swap the parameters like so - array_diff($images, $clean_fetch) it gives me a list of ALMOST all the images in the image folder, which i don't understand. Why does it not give me only the images that are present in the folder but not in the database? Quote Link to comment https://forums.phpfreaks.com/topic/233583-comparing-files-against-database-fields/ Share on other sites More sharing options...
dcro2 Posted April 13, 2011 Share Posted April 13, 2011 mysql_fetch_array returns just what it says on the box, an array. Also, it makes more sense to check for differences AFTER you've gotten all the filenames from the database. Try this: while ($fetch = mysql_fetch_array($results)) { $clean_fetch[] = str_replace("productImages/", "", $fetch[0]); } $diff = array_diff($images, $clean_fetch); foreach (array_unique($diff) as $key => $value) { $output .= $value . "<br />"; } Answer to your last question: $clean_fetch wasn't being setup as an array, so it only had one member ($fetch) every time you looked for differences. Quote Link to comment https://forums.phpfreaks.com/topic/233583-comparing-files-against-database-fields/#findComment-1201092 Share on other sites More sharing options...
laanes Posted April 13, 2011 Author Share Posted April 13, 2011 Hi dcro2 Thank you for your reply! I will give it a try now and get back to you with the results. Thank you for helping me to understand about declaring $clean_fetch as an array first. Also thanks for pointing out that i should only go after the first array index not all of them. Talk to you soon, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233583-comparing-files-against-database-fields/#findComment-1201125 Share on other sites More sharing options...
laanes Posted April 13, 2011 Author Share Posted April 13, 2011 Works like expected! I'd like to delete those images from the folder that appear in that list now. Would it be possible to use array_map to call the unlink function on $images? Or should i be looking at some other way of doing it? Thanks! laanes Quote Link to comment https://forums.phpfreaks.com/topic/233583-comparing-files-against-database-fields/#findComment-1201141 Share on other sites More sharing options...
laanes Posted April 14, 2011 Author Share Posted April 14, 2011 Also, I have tried to join another table with additional images in the query and it seems that i can not compare the fetched results against the folder because the list i get back contains also some images that excist in both, the images folder and the database. Would i have to compare every database table separately against the folder or can i join the tables and compare them together agains the folder? Quote Link to comment https://forums.phpfreaks.com/topic/233583-comparing-files-against-database-fields/#findComment-1201590 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.