Hi, I'm kinda stuck. Currently when you click upload it will spit out number of images found and a list of the names of those images. If I have multiple html files how do I show the names of html files and images associated to that html file? Currently, I have this:
"3 images found:"
a1.gif
a2.gif
3.gif
But, if I have multiple html files I want to show:
6 images found:
test1.html test2.html etc...
a1.gif a4.gif
a2.gif a5.gif
a3.gif a6.gif
I know I would want to use a foreach loop but I'm not sure how/what to use in the html portion. Would I need to write a function for the PHP code below? I have a feeling my code is not the elegant solution.
/* PHP /
$dir = "path/";
$imgs = glob($dir . "*.{jpg,gif,png}", GLOB_BRACE);
foreach($imgs as $img){
$arr1[] = pathinfo( $img,PATHINFO_BASENAME );
};
foreach (glob("path/*.{html, htm}", GLOB_BRACE) as $path){
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($path);
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$src = $image->getAttribute( 'src' );
if(!preg_match('#^(https?|ftp)://#', $src)) {
$arr2[] = pathinfo( $src,PATHINFO_BASENAME );
};
};
};
$diff = array_diff($arr1, $arr2);
$img_dir = new RecursiveDirectoryIterator("path/");
foreach(new RecursiveIteratorIterator($img_dir) as $file) {
if (in_array(basename($file), $diff)) {
$old_dir = 'path/' .$img_dir . '/images/'. basename($file);
$new_dir = 'path/' .$img_dir . '/new_folder/'. basename($file);
if (isset($_POST['submit'])) {
$selected_radio = $_POST['group'];
if ($selected_radio == 'save'){
mkdir('path/' .$img_dir . '/new_folder/', 0700);
rename($old_dir,$new_dir);
}else if($selected_radio == 'delete'){
unlink($old_dir);
};
};
};
};
};
/* HTML /
<form method="post">
<input type="file" name="zip_file" />
<input type="submit" name="upload" value="upload" />
<?php
if (isset($_POST['upload'])) {
foreach(){ // Here I want to show names of html files and a list of found images associated with that html file
echo '<div>'.count($diff). ' images found: '. '</div>';
echo '<div><ul><li>'. implode('</li><li>', $diff) .'</li></ul></div>';
};
}else if (isset($_POST['submit'])){
$selected_radio = $_POST['group'];
if ($selected_radio == 'save'){
echo '<div>'.count($diff). ' images saved to folder!'. '</div>';
}else if($selected_radio == 'delete'){
echo '<div>'.count($diff). ' images deleted!'. '</div>';
};
};
};
?>
<input type="radio" name="group" value="save">Save to folder<br />
<input type="radio" name="group" value="delete">Delete<br /><br />
<input type="submit" name="submit" value="submit" />
</form>