Jump to content

Comparing files against database fields


laanes

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

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.