Web Analytics Made Easy -
StatCounter using scandir or open dir - CodingForum

Announcement

Collapse
No announcement yet.

using scandir or open dir

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Resolved using scandir or open dir

    i have done some testing on both and it appears that scandir is better to use for what i need.

    As i dont need to open any files i just need to see whats in the the dir and i like that scandir puts the files already in alpha order and it also does not include the dir name prefix in the print_r, it just has the array of file names which saves me time.

    Here is what im doing, in my table i have a list of dir file names. What i want to do is compare the name of the file in the table with the name of the file in the dir.

    If the file name exists in the dir but not in the table, i want to run a report list of files not included in the table. (this way i can make sure that what i have not included in the table is accurate)

    If it exists in the table but not in the dir then i need a report that tells me that. (it prob means i deleted a file from the dir and forgot to remove it from the table)


    This whole thing is designed to make sure that dir and table match perfectly unless i choose to not include in the table for good reason.

    i know i will have to pick out . and .. and also mark sub dir folders in the process but.

    Question is. Is scandir what i want to use?
    Last edited by durangod; Sep 7, 2011, 08:19 AM.
    I am not crazy, my computer had me checked but its on dialup and im still waiting for results :)
    A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
    durangod is short for durango dave

  • #2
    scandir does what it says. It is the equivalent of the dir command in dos / ls in linux.
    "Tango says double quotes with a single ( ' ) quote in the middle"
    '$Name says single quotes with a double ( " ) quote in the middle'
    "Tango says double quotes ( \" ) must escape a double quote"
    '$Name single quotes ( \' ) must escape a single quote'

    Comment


    • #3
      thanks tango, i am also considering doing some output to a printer in my reports section the challenge is that i like this code bud i will never know what kind of printer they have so it just wont work without also building a printer input interface page as well.

      PHP Code:

      <?php
      $filename 
      "test.php";
      ///////
      ob_start();
      include 
      $filename;
      $contents ob_get_contents();
      ob_end_clean();
      ///////
      $handle printer_open("SAVIN4045ePCL6");
      printer_set_option($handlePRINTER_MODE"raw");
      printer_write($handle,$contents);
      printer_close($handle);
      ?>
      I know long ago when i worked with fortran, assembly and cobol there was a spooler command you could use which is what im guessing printer_write is doing.

      So what i was thinking is that it might be better to just use the download option to let them download the results to lets say csv format and then they could plug in the report data into whatever they wanted and print it from their local printer.

      Is that what most people do bud? Or is the printer interface a better option?
      Last edited by durangod; Sep 6, 2011, 05:28 PM.
      I am not crazy, my computer had me checked but its on dialup and im still waiting for results :)
      A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
      durangod is short for durango dave

      Comment


      • #4
        No idea.. never tinkered with printers via php. I'd personally just print it all into a html page and then print the page..
        "Tango says double quotes with a single ( ' ) quote in the middle"
        '$Name says single quotes with a double ( " ) quote in the middle'
        "Tango says double quotes ( \" ) must escape a double quote"
        '$Name single quotes ( \' ) must escape a single quote'

        Comment


        • #5
          ok anyway back to the scandir deal.

          here is what i have so far, the first three items in the dir array are

          .
          ..
          .htaccess

          so with this code skip past the . and .. so far so good.

          the problem is that .htaccess shows up twice, so i will need to move the pointer twice not just once if that if statement is true (which is why i have two next calls lol..

          since the pointer is sitting on value, then if i move it once it is on key, so i need to get it to the next value.

          here is the code so far i know its rough lol

          PHP Code:

          $directory 
          '../mydirectory/';

          $files1 scandir($directory);

          foreach(
          $files1 as $key => $value)
           {
              if (
          $value == '.' || $value == '..' || $value == '.htaccess' )
               {
                
          $value next($files1);
                
          //$value = next($files1); removed has to be another way lol

               
          }

               if (
          is_dir("$value"))
                  {
                  
          $markdir "dir - ";
                  
          $value $markdir.$value;
                  }
               
          echo 
          $value;
          echo 
          "<br />";
           }

          exit; 
          resulting in

          .htaccess
          file.php
          file2.php
          file3.php
          dir - cgi-bin

          and it still shows the htaccess file, if i remove the move next option it shows the htaccess twice so im making some progress lol..

          what is strange is that it works fine for . and .. but not for .htaccess
          Last edited by durangod; Sep 6, 2011, 07:32 PM.
          I am not crazy, my computer had me checked but its on dialup and im still waiting for results :)
          A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
          durangod is short for durango dave

          Comment


          • #6
            This is wrong. Next within the scope of the foreach will cause an overwrite continuously until the condition has been passed. Pay closer attention to your results, if you were to just do like so:
            PHP Code:
            $a = array('.''..''.htaccess''file.php''file2.php''file3.php');

            foreach (
            $a AS $i)
            {
                if (
            $i == '.' || $i == '..' || $i == '.htaccess')
                {
                    
            $i next($a);
                }
                print 
            $i '<br />';

            Your results will be as so:
            Code:
            .htaccess
            file.php
            file2.php
            file.php
            file2.php
            file3.php
            Notice the repetition. This is because when . is found, it is replaced by .htaccess. When .. is found, its replaced by file.php. When .htaccess is found, it is replaced b file2.php. And then it continues with its looping since you don't seek the next again.
            In other words, you're not skipping the . or .., you are replacing them.

            That should be using a continue; and not trying to capture the results.
            PHP Code:
            header('HTTP/1.1 420 Enhance Your Calm'); 
            Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

            Comment


            • #7
              Thanks Fou-lu i was playing around with it before i saw your reply and i did notice that the only way i could remove the .htaccess is if i put it in a seperate if and used ===

              thanks for that i will rework it.
              I am not crazy, my computer had me checked but its on dialup and im still waiting for results :)
              A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
              durangod is short for durango dave

              Comment


              • #8
                ok here is the new one, seems to work fine.

                PHP Code:

                $directory 
                '../mydirectory/';

                $files1 scandir($directory);


                foreach(
                $files1 as $key => $value)
                 {
                    if (
                $value == '.' || $value == '..' || $value == '.htaccess')
                     {
                      continue;
                      }

                       if (
                is_dir("$value"))
                        {
                        
                $markdir "dir - ";
                        
                $value $markdir.$value;        
                        }  
                     
                echo 
                $value."<br />";
                 }

                unset(
                $value); 

                exit; 
                Last edited by durangod; Sep 6, 2011, 08:38 PM. Reason: removed last continue dont really need it as it will loop thru and added unset
                I am not crazy, my computer had me checked but its on dialup and im still waiting for results :)
                A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
                durangod is short for durango dave

                Comment


                • #9
                  Durangod.. instead of checking for those files I'd simply just remove them from the array - search for them, remove them and THEN continue working with the file list - works well for me:

                  PHP Code:
                  $NotValidFiles = array('.''..''.htaccess');

                  $Files scandir('./');
                                 
                  foreach(
                  $NotValidFiles as $Item)
                     {
                     if ((
                  $Key array_search($Item$Files)) !== false)
                        {
                        unset(
                  $Files[$Key]);
                        }
                     }
                     
                  //Do your thing with the $Files list below.

                  foreach($Files as $Key => $Value)
                     {
                     
                  //Your code here..
                     

                  Last edited by tangoforce; Sep 6, 2011, 09:33 PM.
                  "Tango says double quotes with a single ( ' ) quote in the middle"
                  '$Name says single quotes with a double ( " ) quote in the middle'
                  "Tango says double quotes ( \" ) must escape a double quote"
                  '$Name single quotes ( \' ) must escape a single quote'

                  Comment


                  • #10
                    Oh yeah, just to remove its actually easier than this:
                    PHP Code:
                    $NotValidFiles = array('.''..''.htaccess');

                    $Files array_diff(scandir('./'), $NotValidFiles); 
                    That looks like it will work.
                    On a side note, I don't think that glob returns any files that start with . since these are typically hidden in a linux environment.
                    PHP Code:
                    header('HTTP/1.1 420 Enhance Your Calm'); 
                    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                    Comment

                    Working...
                    X
                    😀
                    🥰
                    🤢
                    😎
                    😡
                    👍
                    👎