Order Now AdSolution Sign Up | Login » Bits on the Run Sign Up | Login »

Forums

/

Read Sub folder - Sub directory

29 replies [Last post]

I use this code for read the directory of my photo in the rotator:

<?
// search for mp3 files. set this to '.flv' or '.jpg' for the other scripts
$filter = ".jpg";
// path to the directory you want to scan
$directory = "public/photo";

// read through the directory and filter files to an array
@$d = dir($directory);
if ($d) {
while($entry=$d->read()) {
$ps = strpos(strtolower($entry), $filter);
if (!($ps === false)) {
$items[] = $entry;
}
}
$d->close();
sort($items);
}

// third, the playlist is built in an xspf format
// we'll first add an xml header and the opening tags ..
header("content-type:text/xml;charset=utf-8");

echo "<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n";
echo " <title>Sample PHP Generated Playlist</title>\n";
echo " <info>http://www.jeroenwijering.com/</info>\n";
echo " <trackList>\n";

// .. then we loop through the mysql array ..
for($i=0; $i<sizeof($items); $i++) {
echo " <track>\n";
echo " <title>".$items[$i]."</title>\n";
echo " <location>xxx</location>\n";
echo " </track>\n";
}

// .. and last we add the closing tags
echo " </trackList>\n";
echo "</playlist>\n";

?>

this code work correctly, but does not read files in the sub folders...
My folder photo($directory = "public/photo";), contains other 30 folders, how can read the files in subfolders for them to the playlist?

<?php

// *** requires PHP5 ***

// search for jpg files

$filter = ".jpg";

// path to the directory you want to scan
$directory = './';

$it = new RecursiveDirectoryIterator("$directory");

foreach(

new RecursiveIteratorIterator($it) as $file)
{
  if (!((strpos(strtolower($file), $filter)) === false))
  {
    $items[] = preg_replace("#\\\#", "/", $file);
  }
}
sort($items);

header("content-type:text/xml;charset=utf-8");

echo "<?xml version='1.0' encoding='UTF-8'?>
\n";
echo "<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n";
echo "  <title>Sample PHP Generated Playlist</title>\n";
echo "  <info>http://www.jeroenwijering.com/</info>\n";
echo "  <trackList>\n";

foreach($items as $item)
{
  $title_array = explode('/', $item);
  $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));

  echo "    <track>\n";
  echo "      <title>"    . $title . "</title>\n";
  echo "      <location>" . $item  . "</location>\n";
  echo "    </track>\n";
}

echo "  </trackList>\n";
echo "</playlist>\n";

?>

Tanks for the answer, but I have PHP Version 4.3.0

Whit your code have this error:

Fatal error: Cannot instantiate non-existent class: recursivedirectoryiterator in /home/8034033890/www/web/v2/read33.php on line 11

You have a solution for me?

Upgrade to PHP5.

I can not, the server is not mine!

Other solution?

<?php

// search for jpg files
$filter = "jpg";

// path to the directory you want to scan
$directory = './';

$items = scan_directory_recursively($directory, $filter);
$items = array_values_recursive($items);
sort($items);

header("content-type:text/xml;charset=utf-8");

echo "<?xml version='1.0' encoding='UTF-8'?>
\n";
echo "<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n";
echo "  <title>Sample PHP Generated Playlist</title>\n";
echo "  <info>http://www.jeroenwijering.com/</info>\n";
echo "  <trackList>\n";

foreach($items as $item)
{
  if(($item != 'jpg') && (substr(strtolower($item), (strlen($item) - 3), 3) == $filter))
  {
    $title_array = explode('/', $item);
    $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));

    echo "    <track>\n";
    echo "      <title>"    . $title . "</title>\n";
    echo "      <location>" . $item  . "</location>\n";
    echo "    </track>\n";
  }
}

echo "  </trackList>\n";
echo "</playlist>\n";

function array_values_recursive($array)
{
  $flat = array();

  foreach ($array as $value)
  {
    if (is_array($value))
    {
      $flat = array_merge($flat, array_values_recursive($value));
    }
    else
    {
      $flat[] = $value;
    }
  }
  return $flat;
}

// ------------ lixlpixel recursive PHP functions -------------
// scan_directory_recursively( directory to scan, filter )
// expects path to directory and optional an extension to filter
// of course PHP has to have the permissions to read the directory
// you specify and all files and folders inside this directory
// ------------------------------------------------------------

function scan_directory_recursively($directory, $filter)
{
  // if the path has a slash at the end remove it here
  if(substr($directory,-1) == '/')
  {
    $directory = substr($directory,0,-1);
  }

  // if the path is not valid or is not a directory ...
  if(!file_exists($directory) || !is_dir($directory))
  {
    // ... return false and exit the function
    return FALSE;

    // ... else if the path is readable
  }
  elseif(is_readable($directory))
  {
  // open the directory
  $directory_list = opendir($directory);

  // and scan through the items inside
  while (FALSE !== ($file = readdir($directory_list)))
  {
    // if the filepointer is not the current directory
    // or the parent directory
    if($file != '.' && $file != '..')
    {
      // build the new path to scan
      $path = $directory.'/'.$file;

      // if the path is readable
      if(is_readable($path))
      {
        // split the new path by directories
        $subdirectories = explode('/',$path);
        // save the folders name
        $folders_name = substr($subdirectories, $path);
        // if the new path is a directory
        if(is_dir($path))
        {
          // save the folders name
          $folders_name = substr($subdirectories, $path);
          // add the directory details to the file list
          $directory_tree[] = array('path' => $path,
                                    'name' => end($subdirectories),
                                    'kind' => 'directory',
                                    // scan the new path by calling this function
                                    'content' => scan_directory_recursively($path, $filter));

        // if the new path is a file
        }
        elseif(is_file($path))
        {
          // get the file extension by taking everything after the last dot
          $extension = end(explode('.', end($subdirectories)));

          // if there is no filter set or the filter is set and matches
          if($filter === false || $filter == $extension)
          {
            // add the file details to the file list
            $directory_tree[] = array('path' => $path,
                                      'name' => end($subdirectories),
                                      'extension' => $extension,
                                      'size' => filesize($path),
                                      'kind' => 'file');
          }
        }
      }
    }
  }
  // close the directory
  closedir($directory_list); 

  // return file list
  return $directory_tree;

  // if the path is not readable ...
  }
  else
  {
    // ... we return false
    return false; 
  }
}

?>

Produces this:

<?xml version='1.0' encoding='UTF-8'?>
<playlist version='1' xmlns='http://xspf.org/ns/0/'>
  <title>Sample PHP Generated Playlist</title>
  <info>http://www.jeroenwijering.com/</info>
  <trackList>
    <track>
      <title>100k</title>
      <location>./100k.jpg</location>
    </track>
    <track>
      <title>image-1</title>
      <location>./lightbox/images/image-1.jpg</location>
    </track>
    <track>
      <title>thumb-1</title>
      <location>./lightbox/images/thumb-1.jpg</location>
    </track>
    <track>
      <title>logo-1</title>
      <location>./logo-1.jpg</location>
    </track>
    <track>
      <title>logo</title>
      <location>./logo.jpg</location>
    </track>
    <track>
      <title>logo_black</title>
      <location>./logo_black.jpg</location>
    </track>
    <track>
      <title>logo_medium</title>
      <location>./logo_medium.jpg</location>
    </track>
    <track>
      <title>logo_no_transparency</title>
      <location>./logo_no_transparency.jpg</location>
    </track>
    <track>
      <title>logo_small</title>
      <location>./logo_small.jpg</location>
    </track>
    <track>
      <title>11</title>
      <location>./mobilog/files/attachments/11.jpg</location>
    </track>
    <track>
      <title>12</title>
      <location>./mobilog/files/attachments/12.jpg</location>
    </track>
    <track>
      <title>14</title>
      <location>./mobilog/files/attachments/14.jpg</location>
    </track>
    <track>
      <title>15</title>
      <location>./mobilog/files/attachments/15.jpg</location>
    </track>
  </trackList>
</playlist>

Tanks AJAX, i have tried your code... work correctly!
But, have a little problem: the playlist generated container a duble item

Example:

<?xml version='1.0' encoding='UTF-8'?>
<playlist version='1' xmlns='http://xspf.org/ns/0/'>
  <title>Sample PHP Generated Playlist</title>
  <info>http://www.jeroenwijering.com/</info>
  <trackList>
    <track>
      <title>Photo-1</title>
      <location>Photo-1.jpg</location>
    </track>
    <track>
      <title>Photo-1</title>
      <location>MyFolder/Photo/Photo-1.jpg</location>
    </track>
  </trackList>
</playlist>

<location>Photo-1.jpg</location> and <location>MyFolder/Photo/Photo-1.jpg</location> is a same image!
why?

Find this code:

            $directory_tree[] = array('path' => $path,
                                      'name' => end($subdirectories),
                                      'extension' => $extension,
                                      'size' => filesize($path),
                                      'kind' => 'file');

and replace it with this code:            $directory_tree[] = array('path' => $path);

Tanks AJAX.... the your code now is perfect!
1000 Tanks!

Bye

01010501030801160020080401c8baca32ae00e60314006711.jpg

read the mp3 file from directory php5 but 3 warnings occur fread.fseek,fclose supplied arguments not valid

kindly give me any sollution

Ok.. heres a problem for me!!!

I used to have a playlist where i speciefied the folder it was going to look for .jpg. I had an array for the filename and then put that together with an array that gave the http url of that folder. Meaning the actual info that the player used to get the files from became the actual http url.

Now i want the playlist to look through all the subdirs for my gallery instead of me specifying one. So... i use the script above for PHP5 wich works fine.. it generates a playlist and so on... BUT.. the jeroenwijering doesnt seem to find the images now. I look at the location generated and it doesnt show the full http URL byt only the path from my root.

Heres an example of how the current tracks are generated in the playlist:

- <track>
<title>DSC04113</title>
<location>./gallery/g2data/albums/01/DSC04113.jpg</location>
<info>http://www.harleyotooles.com/gallery.php</info>
</track>

I have tried to move the imagerotator AND the playlist to the root. But still it doesnt seem to find the images. So.. how do i fix this? Either i need a way for the player to find the picture from that location info... OR i need to add some code in order for the playlist to actual generate the full http URL path. I tried to make an array for the website url and tried to add it before $item. But im a beginner at this and can only generate an address like this:

www.domain.com/./gallery/g2data/albums/albumdir/filename.jpg

So.. how do i work this one out?
Excuse me if i dont explain so good because i am new to this.. but i hope you understand.

Try this one. Adjust $url as necessary to get teh correct path(s).

<?php

// *** requires PHP5 ***

// search for jpg files
$filter = ".jpg";

// url to be added to the path *** NO TRAILING SLASH ***
$url = 'http://my.domain.com/path';

// path to the directory you want to scan
$directory = './';

$it = new RecursiveDirectoryIterator("$directory");

foreach(new RecursiveIteratorIterator($it) as $file)
{
  if (!((strpos(strtolower($file), $filter)) === false))
  {
    $items[] = preg_replace(array("#\\\#", "#./#"), array("/", "/"), $file);
  }
}
sort($items);

header("content-type:text/xml;charset=utf-8");

print <<<END
<?xml version='1.0' encoding='UTF-8'?>
<playlist version='1' xmlns='http://xspf.org/ns/0/'>
  <title>Sample PHP Generated Playlist</title>
  <info>http://www.mydomain.com/</info>
  <trackList>

END;

foreach($items as $item)
{
  $title_array = explode('/', $item);
  $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
  print <<<END
    <track>
      <title>$title</title>
      <location>$url$item</location>
      <info>http://www.harleyotooles.com/gallery.php</info>
    </track>

END;
}

print <<<END
  </trackList>
</playlist>

END;

?>

I added your info element hard-coded, change it in the heredoc print block if you want something else.

gjghjghjgjgjhjhgjhgjhj

I cannot get this to work no matter what. all I get is a black spinning box on my site. what am I doing wrong? please ask me fr more information, because I do not know what to post and I am getting very VERY upset at this darn thing!!

I am using wordpress, and I created the php file and put my info in for the code... I posted the PHP file and I directed the code in my page to use the php file. I am at a loss and I am getting very angry.

here. it doesnt work with php 5

I'm looking for exactly the same thing , only the asp version.. anyone experience with that ?
Help is much appreciated

Hello,

I am using the php 5 filter playlist posted by AJAX on March 4th 2008.

In my directory that I am scanning I have the files named like;

dolce_and_gabbana.mp4

How can I then get the php file playlist to then strip these and output the file as the tile like;

dolce and gabbana
rather then
dolce_and_gabbana.mp4

Thanks,

Leo

I have

playlists/test.php

the playlist file goes to
../teaser/videos/house/playlist.php

I have teaser/videos/house/playlist.php which autogenerates the playlist from the videos in the directory.

I have this setup inthe playlist.php file

// path to the directory you want to scan
$directory = './';

When I then try and play the video playlist from the playlists/test.php I get an error saying the video is not found

./videofile.mp4

What path directory should I set in the playlist.php script?

Thanks,

Leo

Call playlist.php from your browser for testing, like this:

http://www.domain.com/path/path/playlist.php

Adjust $uri to get the correct path to your media files.

<?php

// *** requires PHP5 ***

// search for jpg files

$filter = ".jpg";

// uri to be added to the path *** NO TRAILING SLASH ***
<strong>$uri = 'http://my.domain.com/path';</strong>

// path to the directory you want to scan
$directory = './';

$it = new RecursiveDirectoryIterator("$directory");

foreach(

new RecursiveIteratorIterator($it) as $file)
{
  if (!((strpos(strtolower($file), $filter)) === false))
  {
    $items[] = preg_replace(array("#\\\#", "#\./#"), array("/", "/"), $file);
  }
}

sort($items);

header("content-type:text/xml;charset=utf-8");

print <<<END
<?xml version='1.0' encoding='UTF-8'?>

<playlist version='1' xmlns='http://xspf.org/ns/0/'>
  <title>Sample PHP Generated Playlist</title>
  <info>http://www.jeroenwijering.com/</info>
  <trackList>

END;

foreach($items as $item)
{
  $title_array = explode('/', $item);
  $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
  $title = str_replace("_", " ", $title);

  print <<<END
    <track>
      <title>$title</title>
      <location>$uri$item</location>
    </track>

END;

}

print <<<END
  </trackList>
</playlist>
END;

?>

Hello Juliette,

Thank you so much it works perfect. If you like Fashion this has been most useful on our website as we can now drop into a folder the latest fashion shows as they happen thanks to you.

Do you ever do any freelance work? As if we need anything in the future then maybe we can consult you?

Thanks,

Leo

leobakerfashion@googlemail.com

Helo Juliette,

Do you know how I can get the filter of flv and mp4 files onto the playlist script as well?

Thanks

Leo

<?php

// *** requires PHP5 ***

// search for files

<strong>$filter1 = ".jpg";
$filter2 = ".flv";
$filter3 = ".mp4";</strong>

// uri to be added to the path *** NO TRAILING SLASH ***
$uri = 'http://my.domain.com/path';

// path to the directory you want to scan
$directory = './';

$it = new RecursiveDirectoryIterator("$directory");

foreach(

new RecursiveIteratorIterator($it) as $file)
{
  <strong>if((strpos(strtolower($file), $filter1) !== false) || (strpos(strtolower($file), $filter2) !== false) || (strpos(strtolower($file), $filter3) !== false))</strong>
  {
    $items[] = preg_replace(array("#\\\#", "#\./#"), array("/", "/"), $file);
  }
}

sort($items);

header("content-type:text/xml;charset=utf-8");

print <<<END
<?xml version='1.0' encoding='UTF-8'?>

<playlist version='1' xmlns='http://xspf.org/ns/0/'>
  <title>Sample PHP Generated Playlist</title>
  <info>http://www.jeroenwijering.com/</info>
  <trackList>

END;

foreach($items as $item)
{
  $title_array = explode('/', $item);
  $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
  $title = str_replace("_", " ", $title);

  print <<<END
    <track>
      <title>$title</title>
      <location>$uri$item</location>
    </track>

END;

}

print <<<END
  </trackList>
</playlist>
END;

?>

Works perfet thank you, also if I have videos like this;

2010_paris_ss10.flv
2010_milan_ss10.flv
2010_mcqueen_paris_ss_2010.mp4

2009_ppq_s09.flv
2009_paris_s09_hightlights.flv
2009_newyork_s09_hightlights.flv

2008_tommy_hilfiger_f08.flv
2008_ralph_lauren_s08.flv
2008_matthew_williamson_s08.flv

How can I order them from 2010 so maybe filter by name DESC?

Thanks,

Leo

 
Sorts alphanumerically by Title in descending order:

<?php

// *** requires PHP5 ***

// search for files filtered by extension
$filter1 = ".jpg";
$filter2 = ".flv";
$filter3 = ".mp4";

// uri to be added to the path *** NO TRAILING SLASH ***
$uri = 'http://my.domain.com/path';

// path to the directory you want to scan
$directory = './';

$it = new RecursiveDirectoryIterator("$directory");

foreach(new RecursiveIteratorIterator($it) as $file)
{
  if((strpos(strtolower($file), $filter1) !== false) || (strpos(strtolower($file), $filter2) !== false) || (strpos(strtolower($file), $filter3) !== false)) 
  
{
    $title_array = explode('\\', $file);
    $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
    $title = str_replace("_", " ", $title);

  //$items[] = preg_replace(array("#\\\#", "#\./#"), array("/", "/"), $file);
    $items[$title] = preg_replace(array("#\\\#", "#\./#"), array("/", "/"), $file);
  }
}

//sort($items);
krsort($items);

header("content-type:text/xml;charset=utf-8");

print <<<END
<?xml version='1.0' encoding='UTF-8'?>
<playlist version='1' xmlns='http://xspf.org/ns/0/'>
  <title>Sample PHP Generated Playlist</title>
  <info>http://www.domain.com/</info>
  <trackList>

END;

//foreach($items as $item)
foreach($items as $key => $item)
{
//$title_array = explode('/', $item);
//$title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
//$title = str_replace("_", " ", $title);

  print <<<END
    <track>
      <title>$key</title>
      <location>$uri$item</location>
    </track>

END;

}

print <<<END
  </trackList>
</playlist>
END;

?>

It's working perfect,

But at the start of each file I have./

./2010 Milan ss10

Thanks,

Leo

Try this foreach() loop:

foreach(new RecursiveIteratorIterator($it) as $file)
{
  if((strpos(strtolower($file), $filter1) !== false) || (strpos(strtolower($file), $filter2) !== false) || (strpos(strtolower($file), $filter3) !== false)) 
  {
    $file = preg_replace(array("#\\\#", "#\./#"), array("/", "/"), $file);

    $title_array = explode('/', $file);
    $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
    $title = str_replace("_", " ", $title);

    $items[$title] = $file;
  }
}

Excellent it is working perfect! :)

Hi, i have a question,
is it possible to make something like this but with downloadlink an titles of the files in a folder on the ftp.
like an automatic download list creator of mp3 files in a specific folder?

i hope that someone can help me