first file from every subfolder!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Effix

    first file from every subfolder!

    How do I get a list of the first file of every subfolder?
    This is what i have so far
    <?php
    function getfirstImage($ dirname)
    {
    global $imageName;
    $ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF",
    "JPEG");
    if($handle = opendir($dirnam e))
    {
    while(false!== ($file = readdir($handle )))
    {
    if(strstr($file , "." . $ext[$i])!= '.' && strstr($file, "." .
    $ext[$i])!= '..')
    {
    break;
    }
    }
    $imageName = $file;
    closedir($handl e);
    }
    return($imageNa me);
    }

    echo getfirstImage(' ../galleri/galleri/diverse/');
    ?>

    $getfirstImage( ../galleri/galleri/)
  • Sjoerd

    #2
    Re: first file from every subfolder!

    On Aug 18, 12:11 pm, Effix <Morteneist...@ gmail.comwrote:
    How do I get a list of the first file of every subfolder?
    To do this for every subfolder, you would make a readdir loop which
    determines whether the file is a directory (is_dir) and call
    getfirstImage() for each directory.

    function getfirstImage($ dirname)
    {
    $imageName = false;
    $ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF",
    "JPEG");
    if($handle = opendir($dirnam e))
    {
    while(false !== ($file = readdir($handle )))
    {
    $lastdot = strrpos($file, '.');
    $extension = substr($file, $lastdot + 1);
    if ($file[0] != '.' && in_array($exten sion,
    $ext))
    {
    break;
    }
    }

    $imageName = $file;
    closedir($handl e);

    }
    return($imageNa me);
    }

    Comment

    • sheldonlg

      #3
      Re: first file from every subfolder!

      Effix wrote:
      How do I get a list of the first file of every subfolder?
      This is what i have so far
      <?php
      function getfirstImage($ dirname)
      {
      global $imageName;
      $ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF",
      "JPEG");
      if($handle = opendir($dirnam e))
      {
      while(false!== ($file = readdir($handle )))
      {
      if(strstr($file , "." . $ext[$i])!= '.' && strstr($file, "." .
      $ext[$i])!= '..')
      {
      break;
      }
      }
      $imageName = $file;
      closedir($handl e);
      }
      return($imageNa me);
      }
      >
      echo getfirstImage(' ../galleri/galleri/diverse/');
      ?>
      >
      $getfirstImage( ../galleri/galleri/)
      Where are you setting/incrementing $i?
      Why aren't you just doing
      if ($file != '.' && $file != '..')
      if those are the two you want to bypass. If you THEN want to check that
      it is one of your extension types, then do a foreach on $ext.

      Comment

      • sheldonlg

        #4
        Re: first file from every subfolder!

        sheldonlg wrote:
        Effix wrote:
        >How do I get a list of the first file of every subfolder?
        >This is what i have so far
        ><?php
        >function getfirstImage($ dirname)
        >{
        >global $imageName;
        >$ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF",
        >"JPEG");
        >if($handle = opendir($dirnam e))
        >{
        >while(false! == ($file = readdir($handle )))
        >{
        >if(strstr($fil e, "." . $ext[$i])!= '.' && strstr($file, "." .
        >$ext[$i])!= '..')
        >{
        >break;
        >}
        >}
        >$imageName = $file;
        >closedir($hand le);
        >}
        >return($imageN ame);
        >}
        >>
        >echo getfirstImage(' ../galleri/galleri/diverse/');
        >?>
        >>
        >$getfirstImage (../galleri/galleri/)
        >
        Where are you setting/incrementing $i?
        Why aren't you just doing
        if ($file != '.' && $file != '..')
        if those are the two you want to bypass. If you THEN want to check that
        it is one of your extension types, then do a foreach on $ext.

        ....also, what happens if a file is named foo.JpG ??

        Comment

        • Captain Paralytic

          #5
          Re: first file from every subfolder!

          On 18 Aug, 11:11, Effix <Morteneist...@ gmail.comwrote:
          How do I get a list of the first file of every subfolder?
          How are you defining "first"?

          Comment

          Working...