Omg. I made this way too hard on myself. All I had to do was this:

mkdir('path/to/image/' .$img_dir . '/new_folder/', 0777);

$img_dir is the variable I want to use not $file_name. Anyway, "new_folder" now sits inside "main_folder." However, even though this code works I'm thinking maybe I should use rename() instead of copy() to "move" files.

$img_dir = new RecursiveDirectoryIterator("path/to/image/"); 
foreach(new RecursiveIteratorIterator($img_dir) as $file) {
    if (in_array(basename($file), $diff)) {
        mkdir('path/to/image/' .$img_dir . '/new_folder/', 0777); 
        $old_dir =  path/to/image/' . $img_dir . '/old_folder/'. basename($file);
        $new_dir = path/to/image/' . $img_dir . '/new_folder/'. basename($file);      
if (copy($old_dir,$new_dir)) { unlink($old_dir); }; }; };

    regarding [man]copy/man vs. [man]rename/man, yes, definitely. copy() takes longer, and it leaves the original file in place (which I don't think is what you intend).

      Thanks! I changed code to rename(). I have another question. The following code will get all the basenames values in img src (and poor attempt at preg_match). However, if I want to ignore/skip over all img src values starting with "http://" and their basenames how would I do that?

      foreach ($images as $image) {
      		$src = $image->getAttribute( 'src' );
      		  if(preg_match($src, '^(http|https|ftp)://') !=0){
              	     $arr[] = pathinfo( $src,PATHINFO_BASENAME ); 
      		  };
      };
        // if(preg_match($src, '^(http|https|ftp)://') !=0)
        if( preg_match( '#^(http|https|ftp)://#',$str ) === false )

          $str should be $src? Doesn't seem to like #. Also tried this but didn't work: if( preg_match( '#src="(http://["]+)#', $src) === false ).

            crump;11012167 wrote:

            $str should be $src?

            YES. sorry for the typo. unfortunately, my [edit post] button has gone.

            crump;11012167 wrote:

            Doesn't seem to like #

            how so? the # character shouldn't be causing problems.

            crump;11012167 wrote:

            Also tried this but didn't work: if( preg_match( '#src="(http://["]+)#', $src) === false ).

            sorry, we should be testing for [FONT=monospace]0[/FONT], not [FONT=monospace]false[/FONT]:

            // if(preg_match($src, '^(http|https|ftp)://') !=0) 
            //if( preg_match( '#^(http|https|ftp)://#',$str ) === false )
            if( preg_match( '#^(http|https|ftp)://#',$src ) === 0 ) // tested. testing is important.

              ...or anything falsy:

              if(!preg_match('#^(https?|ftp)://#', $src)) // returns number of matches (0 or 1) or false in case of error

                Either one works. Thank you very much.

                  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>

                    Figured it out. Was way easier than I thought.

                      I ran into a problem after I echo the file names and image names. How would I compare 1 array against 3 different ones? For example:

                      $arr1 = array(a,b,c,d)
                      $arr2 = array(a,b,c,d,e)
                      $arr3 = array(a,b,c,d,e,f)
                      $arr4 = array(a,b,c,d,e,g)

                      I want to compare:

                      $arr1 and $arr2
                      $arr1 and $arr3
                      $arr1 and $arr4

                      And echo result.

                      So, I did this:

                      $diff = array_diff($arr1, $arr2, $arr3, $arr4);

                      But that just compares everything.

                        To make three separate comparisons ... make three separate comparisons.

                          So, I tried this and came back with 0 image files found. Not sure what else I need to do.

                          $diff1 = array_diff($arr1, $arr2);
                          $diff2 = array_diff($arr1, $arr3);
                          $diff3 = array_diff($arr1, $arr4);

                          $result = array_values($diff1, $diff2, $diff3);
                          //echo $result;

                          $img_dir = new RecursiveDirectoryIterator("path/");
                          foreach(new RecursiveIteratorIterator($img_dir) as $file) {
                          if (in_array(basename($file), $result)) {
                          //code to handle found image files
                          };

                            crump;11012357 wrote:

                            So, I tried this and came back with 0 image files found. Not sure what else I need to do.

                            I'm not sure either. what do you want to do?

                            also, are you sure this

                            $result = array_values($diff1, $diff2, $diff3);
                            echo $result;

                            results in "0 image files found"? It should result in "Array" (and possibly a Notice about array-to-string conversion).

                              Sorry, long day and I think I may be over-thinking this. If there are certain number of found unused images in test1.html these same images would also show up in test2.html. Unless, of course, if some of those images are used in test2.html then they wouldn't be "unused" any longer. Wouldn't there be fewer images found in test2.html than test1.html as a result? I'm getting duplicate number of images in both html files.

                                traq;11012365 wrote:

                                I'm not sure either. what do you want to do?

                                also, are you sure this

                                $result = array_values($diff1, $diff2, $diff3);
                                echo $result;

                                results in "0 image files found"? It should result in "Array" (and possibly a Notice about array-to-string conversion).

                                Actually, it would result in a null, as well as an error message about how [man]array_values[/man] expects exactly one parameter instead of the three that it was given (presumably [man]array_merge[/man] was intended).

                                  could you [man]print_r/man everything and show us what results you are getting? then, show us what you expect/want?

                                  Weedpacket wrote:

                                  Actually, it would result in a null, as well as an error message about how array_values expects exactly one parameter instead of the three that it was given (presumably array_merge was intended).

                                  right... misread that (long day also)

                                    I think I got it to work. Thanks and sorry for the confusion.

                                    I have one other question...last question I really hope. Is there a way to set a variable to take any directory path?

                                    For instance this code:

                                    $dir = "$dir_path//";
                                    $images = glob($dir . "*.{jpg,gif,png}", GLOB_BRACE);

                                    can be set so that $dir can take any directory?

                                    $dir = "$dir_path//"; or "$dir_path*/";

                                    Thanks!

                                      try it?

                                      the * means nothing to the variable itself, but [man]glob/man will interpret it as a wildcard, yes - just as if you'd typed it as an argument directly.

                                        Still can't get it work.

                                        $a = "$dir_path*/*/";
                                        $b = "$dir_path*/";  
                                        $dir = $a || $b; $imgs = glob($dir . "*.{jpg,gif,png}", GLOB_BRACE); echo $dir;

                                        Also tried these variations with no luck:
                                        $dir $a or $b;
                                        $dir .= $b .= $a;
                                        $dir = $b = $a;