fileorder of readdir()

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

    fileorder of readdir()

    Hi NG

    In wich order does "readdir()" read files from the disc? I've got an image
    folder with images in the format "00001.jpg" , "00002.jpg" etc. It seems
    "readdir()" read the lowest first, but is it certain?

    An alternative is to read the files into an array and sort them, does anyone
    have any experience with this? Should I use the "SORT_REGUL AR",
    "SORT_NUMER IC" or "SORT_STRIN G"?

    Thanks alot


    Med Venlig Hilsen

    Ask Josephsen
    Partner og IT

    web: www.MinReklame.dk


  • Andy Hassall

    #2
    Re: fileorder of readdir()

    On Mon, 23 Aug 2004 16:01:24 +0200, "Ask Josephsen" <ask(((at)))min reklame.dk>
    wrote:
    [color=blue]
    >In wich order does "readdir()" read files from the disc? I've got an image
    >folder with images in the format "00001.jpg" , "00002.jpg" etc. It seems
    >"readdir()" read the lowest first, but is it certain?[/color]

    No. It's operating system and presumably filesystem dependent and totally
    unreliable.
    [color=blue]
    >An alternative is to read the files into an array and sort them, does anyone
    >have any experience with this? Should I use the "SORT_REGUL AR",
    >"SORT_NUMERI C" or "SORT_STRIN G"?[/color]

    SORT_NUMERIC doesn't make any sense unless your filenames are all numbers. The
    examples you gave aren't.

    If you're sorting filenames, look at natsort - it gives a much more intuitive
    sort order if your filenames aren't strictly formatted and zero-padded.

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Gordon Burditt

      #3
      Re: fileorder of readdir()

      >In wich order does "readdir()" read files from the disc? I've got an image[color=blue]
      >folder with images in the format "00001.jpg" , "00002.jpg" etc. It seems
      >"readdir()" read the lowest first, but is it certain?[/color]

      Any order it wants to. The order may seem less straightforward after
      you have deleted and added files for a while. If you did something
      like "cp *.jpg /usr/local/apache/data/images" it might appear you
      get them in order by name, but that's not guaranteed.
      [color=blue]
      >An alternative is to read the files into an array and sort them, does anyone
      >have any experience with this? Should I use the "SORT_REGUL AR",
      >"SORT_NUMERI C" or "SORT_STRIN G"?[/color]

      If you want a specific order, sort the files.
      In what order do you want the files?

      If you had file names like:
      1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg, 6.jpg, 7.jpg, 8.jpg, 9.jpg, 10.jpg, 11.jpg ...

      you might be concerned about whether 10.jpg and 11.jpg come between
      1.jpg and 2.jpg, or whether they show up after 9.jpg. With the
      names you have given, with a fixed number of digits, it won't
      matter which sort type you use.

      Gordon L. Burditt

      Comment

      • steve

        #4
        Re: Re: fileorder of readdir()

        "Gordon Burditt60" wrote:[color=blue][color=green]
        > >In wich order does "readdir()" read files from the disc?[/color]
        > I’ve got an image[color=green]
        > >folder with images in the format "00001.jpg" , "00002.jpg" etc.[/color][/color]
        It[color=blue]
        > seems[color=green]
        > >"readdir()" read the lowest first, but is it certain?[/color]
        >
        > Any order it wants to. The order may seem less straightforward[/color]
        after[color=blue]
        > you have deleted and added files for a while. If you did something
        > like "cp *.jpg /usr/local/apache/data/images" it might appear you
        > get them in order by name, but that’s not guaranteed.
        >[color=green]
        > >An alternative is to read the files into an array and sort them,[/color]
        > does anyone[color=green]
        > >have any experience with this? Should I use the "SORT_REGUL AR",
        > >"SORT_NUMERI C" or "SORT_STRIN G"?[/color]
        >
        > If you want a specific order, sort the files.
        > In what order do you want the files?
        >
        > If you had file names like:
        > 1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg, 6.jpg, 7.jpg, 8.jpg, 9.jpg,[/color]
        10.jpg,[color=blue]
        > 11.jpg ...
        >
        > you might be concerned about whether 10.jpg and 11.jpg come between
        > 1.jpg and 2.jpg, or whether they show up after 9.jpg. With the
        > names you have given, with a fixed number of digits, it won’t
        > matter which sort type you use.
        >
        > Gordon L. Burditt</font>[/color]

        If you want to do something really fancy, you can always escape to
        shell to do it, like:
        $a = `ls -l`;
        then parse $a which holds the result that came back.

        --
        http://www.dbForumz.com/ This article was posted by author's request
        Articles individually checked for conformance to usenet standards
        Topic URL: http://www.dbForumz.com/PHP-fileorde...ict142308.html
        Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=476406

        Comment

        • Andy Hassall

          #5
          Re: fileorder of readdir()

          On 23 Aug 2004 17:21:26 -0400, steve <UseLinkToEmail @dbForumz.com> wrote:
          [color=blue]
          >If you want to do something really fancy, you can always escape to
          >shell to do it, like:
          >$a = `ls -l`;
          >then parse $a which holds the result that came back.[/color]

          Ugh - you then have the following problems:

          (1) Worries about shell injection attacks if you pass any parameters.
          (2) Are you sure 'ls' on your PATH does what you want?
          (3) Extra overhead of spawning off new processes per reqest.
          (4) Loss of portability - ls doesn't exist on Windows.
          (5) Is the output of ls even standardised? Across all versions of Unix?
          (6) Does it sort in the way wanted by the OP?

          You're almost certainly better off sticking to readdir and sort it afterwards.

          --
          Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
          <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

          Comment

          • Ask Josephsen

            #6
            Re: fileorder of readdir()

            Thanks alot - your comments are very helpfull :)

            --

            Med Venlig Hilsen

            Ask Josephsen
            Partner og IT

            web: www.MinReklame.dk


            "Ask Josephsen" <ask(((at)))min reklame.dk> wrote in message
            news:4129f8b6$0 $213$14726298@n ews.sunsite.dk. ..[color=blue]
            > Hi NG
            >
            > In wich order does "readdir()" read files from the disc? I've got an image
            > folder with images in the format "00001.jpg" , "00002.jpg" etc. It seems
            > "readdir()" read the lowest first, but is it certain?
            >
            > An alternative is to read the files into an array and sort them, does[/color]
            anyone[color=blue]
            > have any experience with this? Should I use the "SORT_REGUL AR",
            > "SORT_NUMER IC" or "SORT_STRIN G"?
            >
            > Thanks alot
            >
            >
            > Med Venlig Hilsen
            >
            > Ask Josephsen
            > Partner og IT
            >
            > web: www.MinReklame.dk
            >
            >[/color]


            Comment

            • Varavva Yevgen aka xa4anypu

              #7
              Re: fileorder of readdir()


              ----- Original Message -----
              From: "Ask Josephsen" <ask(((at)))min reklame.dk>
              Newsgroups: comp.lang.php
              Sent: Monday, August 23, 2004 5:01 PM
              Subject: fileorder of readdir()

              [color=blue]
              > Hi NG
              >
              > In wich order does "readdir()" read files from the disc? I've got an image
              > folder with images in the format "00001.jpg" , "00002.jpg" etc. It seems
              > "readdir()" read the lowest first, but is it certain?
              >
              > An alternative is to read the files into an array and sort them, does[/color]
              anyone[color=blue]
              > have any experience with this? Should I use the "SORT_REGUL AR",
              > "SORT_NUMER IC" or "SORT_STRIN G"?[/color]

              Try to put all folder content with readdir() to an array, than usort() with
              this function:
              function cmp ($el_1,$el_2)
              {
              if(is_dir($el_1 ) && !is_dir($el_2)) return -1;
              if(!is_dir($el_ 1) && is_dir($el_2)) return 1;
              if($el_1<$el_2) return -1;
              elseif($el_1>$e l_2) return 1;
              else return 0;
              }





              Comment

              • Ask Josephsen

                #8
                Re: fileorder of readdir()

                Thanks for all your tips :)

                definately helped me out

                --

                Med Venlig Hilsen

                Ask Josephsen
                Partner og IT

                web: www.MinReklame.dk


                "Ask Josephsen" <ask(((at)))min reklame.dk> wrote in message
                news:4129f8b6$0 $213$14726298@n ews.sunsite.dk. ..[color=blue]
                > Hi NG
                >
                > In wich order does "readdir()" read files from the disc? I've got an image
                > folder with images in the format "00001.jpg" , "00002.jpg" etc. It seems
                > "readdir()" read the lowest first, but is it certain?
                >
                > An alternative is to read the files into an array and sort them, does[/color]
                anyone[color=blue]
                > have any experience with this? Should I use the "SORT_REGUL AR",
                > "SORT_NUMER IC" or "SORT_STRIN G"?
                >
                > Thanks alot
                >
                >
                > Med Venlig Hilsen
                >
                > Ask Josephsen
                > Partner og IT
                >
                > web: www.MinReklame.dk
                >
                >[/color]


                Comment

                Working...