Go
Not registered? Sign up!

Read Sub folder - Sub directory

Google Translate
18 posts | return to the Modules forum | get the rss feed for this thread

Mar. 04, 2008ForceOne

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?

Mar. 04, 2008AJAX

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

?>

Mar. 05, 2008ForceOne

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?

Mar. 05, 2008jazzie

Upgrade to PHP5.

Mar. 05, 2008ForceOne

I can not, the server is not mine!

Mar. 05, 2008ForceOne

Other solution?

Mar. 05, 2008AJAX

<?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>

Mar. 05, 2008ForceOne

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?

Mar. 05, 2008AJAX

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);

Mar. 05, 2008ForceOne

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

Bye

Apr. 03, 2008picture

01010501030801160020080401c8baca32ae00e60314006711.jpg

Apr. 21, 2008venkatesh

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

kindly give me any sollution

Sep. 20, 2008beekay

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.

Sep. 20, 2008kLink

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.

Jan. 03, 2009ghhj

gjghjghjgjgjhjhgjhgjhj

Mar. 18, 2009aboyd

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.

Apr. 25, 2009same

here. it doesnt work with php 5

Aug. 26, 2009Echelon

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

Add a reaction

You can also return to the category or try this search for related threads.


 

Search the Forums

Go

Support

Support Here are some helpful links to learn more about the JW Player™:

Monetize Your Video

Monetize Your Video Earn money with ads from LongTail's AdSolution. Watch our demos and sign up now!

Why Buy a License?

Why Buy a License? If you don’t buy a commercial license, you cannot use a JW Player™ on (i) a site that has ads; (ii) a corporate site; or a (iii) CMS. Our licenses are very inexpensive, so what are you waiting for? Buy a license today.