mikem562 Posted August 13, 2009 Share Posted August 13, 2009 I have the following code which goes through a dir and writes an XML file based on the images inside that folder. I'm encountering two problems. The first is I want it to only select images that end with $file_ext which happens to be _img . I don't want it to select any other photos. Secondly I have this: $file_img = "$file"; < This will be the name of a file for example jan01_img.jpg $file_tmb = "INSERT CODE HERE"; < I want this to keep the file name and replace _img with _tmb. Any help would be muchly appreciated. Code can be found below <?php $dir = '../galleries/music/'; $file_type = "jpg"; $file_ext = "_img"; $play_list = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; $play_list .= "<content>\n"; $play_list .= "<gallery>\n"; // Open directory, read contents and add to file_list if correct file_type if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ($file != '.' && $file != '..') { $name_array = explode('.', $file); if ($name_array[1] == $file_type) { $file_img = "$file"; $file_tmb = "INSERT CODE HERE"; $play_list .= "<image Thumb=\"$file_tmb\" Large=\"$file_img\"></image>\n"; } } } closedir($dh); $play_list .= "</gallery>\n"; $play_list .= "</content>\n"; $wdir = ''.$dir.'images1.xml'; $f=fopen("$wdir","w") or die("Could not open and empty the file"); fwrite($f,$play_list) or die("Could not write to the file"); fclose($f) or die ("Could not close the resource"); } } ?> Quote Link to comment Share on other sites More sharing options...
mikem562 Posted August 13, 2009 Author Share Posted August 13, 2009 I've got the replace function working now and I have it only selecting the files I need now. My new question is . Is there a way to set a base dir so $dir = '../galleries'; and have some type of forloop that goes through every subdir until it finds a *_img.jpg and when it does, it process's that dir generates the xml there and moves on to the next? <?php $dir = '../galleries/music/'; $file_ext = "img.jpg"; $play_list = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; $play_list .= "<content>\n"; $play_list .= "<gallery>\n"; // Open directory, read contents and add to file_list if correct file_type if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ($file != '.' && $file != '..') { $name_array = explode('_', $file); if ($name_array[1] == $file_ext) { $file_img = "$file"; $file_tmb = str_replace("_img", "_tmb", "$file"); $play_list .= "<image Thumb=\"$file_tmb\" Large=\"$file_img\"></image>\n"; } } } closedir($dh); $play_list .= "</gallery>\n"; $play_list .= "</content>\n"; $wdir = ''.$dir.'images1.xml'; $f=fopen("$wdir","w") or die("Could not open and empty the file"); fwrite($f,$play_list) or die("Could not write to the file"); fclose($f) or die ("Could not close the resource"); } } ?> Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Share Posted August 13, 2009 There might be some more elegant ways ofdoing this but this does it also.. <?php $file = 'test_img.jpg'; $tmb_file = str_replace('_img', '_tmb', $file); var_dump($tmb_file); // "test_tmb.jpg" Quote Link to comment Share on other sites More sharing options...
mikem562 Posted August 13, 2009 Author Share Posted August 13, 2009 Thanks TeNDoLLA Have you any ideas for my last question? I edited the above post ... Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Share Posted August 13, 2009 I have zero experience on handling dirs so I am not sure if I can help with that much. But if you want to take a look here is a thread discussing about looping through directories which is pretty complicated if you have no experience before of using the iterators: http://www.sitepoint.com/forums/showthread.php?t=626691&highlight=directoryiterator . But if you have or get somehow the dirs in array then you could do something like this <?php $basedir = '../gallery/' $subDirs = array('music', 'images', 'images2'); foreach ($dirs as $dir) { $currentDir = $basedir . $dir; // here loop through the files // in currentDir and create xml } Also when looping the files through you can use is_dir() function to check if a file is a dir and then you could go into that dir and loop its files again. About creating the xml I already posted to your another thread earlier: http://www.phpfreaks.com/forums/index.php/topic,264713.0.html . Quote Link to comment Share on other sites More sharing options...
mikem562 Posted August 13, 2009 Author Share Posted August 13, 2009 awesome!! Thank you, I'll be sure to post the final result incase anyone else should care to use it =), now just working on sorting the xml / image nodes by date so the newest is always at the top haha and then it will be set! Quote Link to comment 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.