Finding Unlinked Images

images php coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Wed Aug 15, 2012 9:39 pm

Finding Unlinked Images

Postby crump » Wed Aug 15, 2012 9:57 pm


I want to build a php form that would upload and unzip a folder that contains some static html files and a shared image folder. This part is done. Next, I need to compare the img src in the html files with the images in the image folder. If images exist don't do anything, else save unused images in a temp folder or delete.

Here's my code. I can extract images from the shared image folder and the relative image src path from the html files but I don't know how to compare/check the two. Do I check the html file against the image folder or the other way around? Not sure what PHP functions to use(file_exist, getimagesize, curl, etc.). Please help.

//path to directory to scan
$dir = "path/to/image/directory";

//get all image files with a .jpg, .gif, .png extension.
$images = glob($dir . "*.{jpg,gif,png}", GLOB_BRACE);

//print images in image folder - for testing purpose spits out images/1.jpg, images/2.png, images/3.gif
foreach($images as $image)
echo 'images/'. basename($image) . '<br />';

//extract img src from html

$html = file_get_contents($url);
$dom = new DOMDocument();

//echo image src - for testing purpose spits out images/1.jpg, images/2.png, images/3.gif
foreach ($dom->getElementsByTagName('img') as $img) {
echo $img->getAttribute('src'). '<br />';


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 2 guests