Jump to content

[SOLVED] Strip/Change FileName...


mikem562

Recommended Posts

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");        
          }
       }
    ?>

Link to comment
Share on other sites

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");
                 
          }
       }
       
    ?>

Link to comment
Share on other sites

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 .

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.