Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts

    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; 09-07-2011 at 01:19 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    scandir does what it says. It is the equivalent of the dir command in dos / ls in linux.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    durangod (09-06-2011)

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts
    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; 09-06-2011 at 10:28 PM.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    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..
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts
    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; 09-07-2011 at 12:32 AM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    durangod (09-07-2011)

  • #7
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts
    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; 09-07-2011 at 01:38 AM. Reason: removed last continue dont really need it as it will loop thru and added unset

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    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; 09-07-2011 at 02:33 AM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    durangod (09-07-2011)

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    durangod (09-07-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •