Select random files out of a directory

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

    #1

    Select random files out of a directory

    for example, there are 10K files in the directory,

    given that i don't know if files name, is it possible to fetch a file
    randomly?

    thanks.

  • Hendri Kurniawan

    #2
    Re: Select random files out of a directory

    Yes...
    See opendir, readdir, rand
    And get creative

    Hendri Kurniawan


    howa wrote:
    for example, there are 10K files in the directory,
    >
    given that i don't know if files name, is it possible to fetch a file
    randomly?
    >
    thanks.
    >

    Comment

    • howa

      #3
      Re: Select random files out of a directory


      Hendri Kurniawan ¼g¹D¡G
      Yes...
      See opendir, readdir, rand
      And get creative
      >
      Hendri Kurniawan
      >
      >
      howa wrote:
      for example, there are 10K files in the directory,

      given that i don't know if files name, is it possible to fetch a file
      randomly?

      thanks.
      the problem is:

      there are too many files in the folder, e.g. over 10K or more, i don't
      want to list it

      but i know that it must contains 10K files, so it is possible to get ,
      say 1020th file under this folder?

      Comment

      • Skeleton Man

        #4
        Re: Select random files out of a directory

        >the problem is:
        >there are too many files in the folder, e.g. over 10K or more, i don't
        >want to list it
        >but i know that it must contains 10K files, so it is possible to get ,
        >say 1020th file under this folder?
        Use scandir() to return an array of filenames and array_rand() to pick a
        random entry from that list.. (see the manual for details)

        Chris


        Comment

        • Hendri Kurniawan

          #5
          Re: Select random files out of a directory

          Unless other people knows any other methods.

          for($i = 0; $i < 1024; $i++) $fileName = readdir($dirHan dler);
          print $fileName;

          Now if you know the file names, that is another matter.

          Hendri Kurniawan

          howa wrote:
          Hendri Kurniawan ¼g¹D¡G
          >
          >Yes...
          >See opendir, readdir, rand
          >And get creative
          >>
          >Hendri Kurniawan
          >>
          >>
          >howa wrote:
          >>for example, there are 10K files in the directory,
          >>>
          >>given that i don't know if files name, is it possible to fetch a file
          >>randomly?
          >>>
          >>thanks.
          >>>
          >
          the problem is:
          >
          there are too many files in the folder, e.g. over 10K or more, i don't
          want to list it
          >
          but i know that it must contains 10K files, so it is possible to get ,
          say 1020th file under this folder?
          >

          Comment

          • Hendri Kurniawan

            #6
            Re: Select random files out of a directory

            BTW scandir is not available prior to PHP5
            Just a note

            Hendri Kurniawan

            Skeleton Man wrote:
            >the problem is:
            >there are too many files in the folder, e.g. over 10K or more, i don't
            >want to list it
            >but i know that it must contains 10K files, so it is possible to get ,
            >say 1020th file under this folder?
            >
            Use scandir() to return an array of filenames and array_rand() to pick a
            random entry from that list.. (see the manual for details)
            >
            Chris
            >
            >

            Comment

            • Martin Mandl - m2m tech support

              #7
              Re: Select random files out of a directory

              If you have access to system commands you could try the following:

              1) Get the number of files using something like: ls | wc
              2) Use your random generator
              3) Now pick your line e.g. for file # 123: ls | head -123 | tail -1

              Have fun


              howa wrote:
              for example, there are 10K files in the directory,
              >
              given that i don't know if files name, is it possible to fetch a file
              randomly?
              >
              thanks.

              Comment

              • Curtis

                #8
                Re: Select random files out of a directory

                $dir = './';
                $files = array();
                $file = '';
                if ( is_dir($dir) ) {
                if ( $d = opendir($dir) ) {
                while ( ($file = readdir($d)) !== false ) {
                if ( !is_dir($file) ) $files[] = $file;
                }
                }
                }

                // specify an int for the optional 2nd arg if you
                // need more than one random file
                $randomKeys = array_rand($fil es);
                $randomFile = $files[$randomKeys[0]];

                $files holds an array of all files in the directory specified in the
                $dir variable. This can take the place of scandir if you're running on
                a version of PHP prior to 5.x.

                On Jan 4, 9:30 pm, Hendri Kurniawan <ask...@email.c omwrote:
                BTW scandir is not available prior to PHP5
                Just a note
                >
                Hendri Kurniawan
                >
                Skeleton Man wrote:
                the problem is:
                there are too many files in the folder, e.g. over 10K or more, i don't
                want to list it
                but i know that it must contains 10K files, so it is possible to get ,
                say 1020th file under this folder?
                >
                Use scandir() to return an array of filenames and array_rand() to pick a
                random entry from that list.. (see the manual for details)
                >
                Chris

                Comment

                • howa

                  #9
                  Re: Select random files out of a directory


                  Martin Mandl - m2m tech support ¼g¹D¡G
                  If you have access to system commands you could try the following:
                  >
                  1) Get the number of files using something like: ls | wc
                  2) Use your random generator
                  3) Now pick your line e.g. for file # 123: ls | head -123 | tail -1
                  >
                  Have fun
                  >
                  >
                  howa wrote:
                  for example, there are 10K files in the directory,

                  given that i don't know if files name, is it possible to fetch a file
                  randomly?

                  thanks.
                  thanks....all examples should work for me...but a little bit slow as my
                  folder contains more than 10K files....thanks anyway

                  Comment

                  • Martin Mandl - m2m tech support

                    #10
                    Re: Select random files out of a directory

                    Another idea: Why don't you load the names of your files into a
                    database. If you do this with an automatic task, it does not matter how
                    long it takes. ... and getting a random result form the database should
                    not take that long ...

                    howa wrote:
                    thanks....all examples should work for me...but a little bit slow as my
                    folder contains more than 10K files....thanks anyway

                    Comment

                    • Stefan Rybacki

                      #11
                      Re: Select random files out of a directory

                      -----BEGIN PGP SIGNED MESSAGE-----
                      Hash: SHA1

                      Curtis schrieb:
                      $dir = './';
                      $files = array();
                      $file = '';
                      if ( is_dir($dir) ) {
                      if ( $d = opendir($dir) ) {
                      while ( ($file = readdir($d)) !== false ) {
                      if ( !is_dir($file) ) $files[] = $file;
                      }
                      }
                      }
                      >
                      // specify an int for the optional 2nd arg if you
                      // need more than one random file
                      $randomKeys = array_rand($fil es);
                      $randomFile = $files[$randomKeys[0]];
                      >
                      >
                      Without the memory overhead selecting one random file could in addition
                      look like this then:

                      $dir = './';
                      $file = '';
                      mt_srand(time() );
                      $probability=mt _getrandmax()/10000;
                      if ( is_dir($dir) ) {
                      if ( $d = opendir($dir) ) {
                      while ( ($file = readdir($d)) !== false ) {
                      if ( !is_dir($file) ) {
                      if (mt_rand()<$pro bability) break;
                      }
                      }
                      }
                      }

                      I know it's far from perfect and it's not equally distributed but might
                      save some execution time if you accidently select the second file ;) as
                      well as you don't have the memory overhead and it takes at most as long
                      as the original script. Important here is the probability variable,
                      which should be adjusted with an estimated file count of the desired folder.

                      Stefan
                      -----BEGIN PGP SIGNATURE-----
                      Version: GnuPG v1.4.2.1 (MingW32)

                      iD8DBQFFnnuRyeC Lzp/JKjARAgImAJ0WdU 9IjvrF9+vgYqRGQ k3ewvjW/ACfcrAx
                      i7zjE6lnU58u6Ss ykMualQs=
                      =4QPJ
                      -----END PGP SIGNATURE-----

                      Comment

                      Working...