Read files in a folder

My application has a video with the name of the folder. I want to raed the entire file only in this folder, which I made almost, it does not show any folder in the video folder, except for the .DS_Store I currently applied, if the condition is to hide it, but I want the perfect solution. can someone help: - my code is: ----

$dir = "../videos/";
$dh  = opendir($dir);

print '<select size="4" name="vidfile">';

while (($file = readdir($dh)) !== false) {

    if (is_file($dir.'/'.$file) && $file != "." && $file != "..") {

        if ($file!=".DS_Store") {

            print "<option>{$file}</option>";
        }
    }          
}

print '</select>'; 

closedir($dh);
+3
source share
4 answers

Quick and easy solution

You can make your life easier and use DirectoryIteratorto go through the directory.

echo '<select name="vids" size="4">';
foreach( new DirectoryIterator('/path/to/videos') as $file) {
    if( $file->isFile() === TRUE && $file->getBasename() !== '.DS_Store') {
        printf("<option>%s</option>\n", htmlentities($file->getBasename()));
    }
}
echo '</select>';

Enhancement: decoupling directory filtering from a SelectBox building

foreach, a FilterIterator , accept(). DirectoryIterator FilterIterator. , , :

class MyFilter extends FilterIterator
{
    public function accept()
    {
        return $this->current()->isFile() === TRUE &&
               $this->current()->getBasename() !== '.DS_Store';
    }
}

$iterator = new MyFilter(new DirectoryIterator('/path/to/videos'));

foreach , accept(). accept() FALSE, .

SelectBox :

echo '<select name="vids" size="4">';
foreach( $iterator as $file) {
    printf("<option>%s</option>\n", htmlentities($file->getBasename()));
}
echo '</select>';

FilterIterator

FilterIterator , , , SelectBox, FilterChainIterator :

$iterator = new FilterChainIterator(new DirectoryIterator('/path/to/videos'));
$iterator->addCallback(function($file) {
    return $file->isFile() === TRUE &&
           $file->getBasename() !== '.DS_Store';}); 

SelectBox , .


: SelectBox

, SelectBox , . , DOM HTML. Iterator, HTML , render() :

class SelectBox
{
    protected $iterator;
    public function __construct(Iterator $iterator)
    {
        $this->iterator = $iterator;
    }
    public function render()
    {
        $dom = new DOMDocument;
        $dom->formatOutput = TRUE;
        $dom->loadXml('<select name="vids"/>');
        $dom->documentElement->appendChild(new DOMElement('option', 'Pick One'));
        foreach($this->iterator as $option) {
            $dom->documentElement->appendChild(
                new DOMElement('option', $option));
        }
        return $dom->saveXml($dom->documentElement);
    }
    public function __toString()
    {
        return $this->render();
    }
}

SelectBox Iterator ,

echo new SelectBox(new MyFilter(new DirectoryIterator('/path/to/videos')));

, , . ,

echo new SelectBox(new ArrayIterator(array('foo', 'bar', 'baz')));

<select>
  <option>Pick One</option>
  <option>foo</option>
  <option>bar</option>
  <option>baz</option>
</select>
+13
<?php
$dir = '../videos/';
if($dh = opendir($dir)){
   echo '<select size="4" name="vidfile">';
   while(($file=readdir($dh)) !== FALSE){
      $file = $dir . '/' . $file;
      if(!is_file($file) || substr($file, 0, 1) == '.'){
         continue;
      }
      printf('<option>%s</option>', htmlentities($file));
   }
   echo '</select>';
   closedir($dh);
}
?>

-, opendir , . () , (.,..,.htaccess ..).

+2

, glob(), option . , glob() , .

$dir = "../videos";
$mask = "*";
// $mask = "*.{wmv,mpeg,mpg}";  // get only files of these types

$files = glob($dir."/".$mask);
$excluded_files = array(".DS_store");
$html = null;    

if (count($files) > 0)
foreach ($files as $file)
 {
  $filename = basename($file);
  if (in_array($filename, $excluded_files)) continue;
  $html.="<option>".htmlentities($filename)."</option>";
 }

 echo $html;
+1

if ($file[0] == ".") continue; 2, , , , .
, 2 .
:

<?php
  $dir = "../videos/";
  $dh = opendir($dir);
  $filesarr = array();
  while (($file = readdir($dh)) !== false) {
    if ($file[0] != ".") $filesarr[] = $file;
  }
  closedir($dh);
?>

:

<select size="4" name="vidfile">
<?php foreach ($filesarr as $file): ?>
<option><?php echo $file ?></option>
<?php endforeach ?>
</select>

.

0
source

Source: https://habr.com/ru/post/1761470/


All Articles