A problem with accessing files and directories via PHP

Discussion in 'Apache' started by narek, Jul 5, 2011.

  1. #1
    Hello there .I have installed XAMPP1.7.4 on my Windows XP Professional . I have wrote a function that outputs directories and files in a particular directory. I have changed none of XAMPP's folders or files.

    But my problem is that it works for some directories and for some doesnt.
    Here is my code
    <?php

    function getDirectoryList ($directory)
    {

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // open directory and walk through the filenames
    while ($file = readdir($handler)) {

    // if file isn't this directory or its parent, add it to the results
    if ($file != "." && $file != "..") {
    $results[] = $file;
    }

    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

    }// the end of the definition of the function


    function get_file_extension($file_name)// Return the extension of file
    {
    return substr(strrchr($file_name,'.'),1);
    }




    $ext="png";
    $initial_dir="C:\xampp";


    $d=0;
    $parts = explode("\\",$initial_dir);

    $last=$parts[count($parts)-1];
    $all_files_ar=getDirectoryList($initial_dir);
    echo count($all_files_ar)."<br/><br/>";
    for($i=0;$i<count($all_files_ar);$i++)
    {
    echo $all_files_ar[$i]."<br/>";

    }


    $myFile = "testFile.xml";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "<{$last}>\n";
    fwrite($fh, $stringData);

    for($i=0;$i<count($all_files_ar);$i++)
    {



    $sub_dir=$initial_dir."\\".$all_files_ar[$i];
    $current_ext=get_file_extension($sub_dir);

    if($current_ext==$ext)
    {
    $expl_array=explode("\\",$sub_dir);
    $last_file= $expl_array[count($expl_array)-1];

    $stringData ="<file name=\"{$last_file}\" />\n";
    fwrite($fh, $stringData);

    }




    }
    $stringData = "</{$last}>\n";
    fwrite($fh, $stringData);
    fclose($fh);

    ?>





    For example when i want to output my desktop's folders and files it works..but for "C:\xampp" directory it shows the following
    It gives me a similar error when i when i call the function with this directory "C:\BrowserPlusPlugins\1eadddfde3bc2ed8a5327d925903fec1"



    And for some directories it outputs invisible files...I mean when i take a look at a directory i see 3 files but when i output it appears 5 ..So 2 additional files the first is called "Folder.jpg" the second blabla.docx and i think the last one is inserted by Administrator ,because i found there couple unreadable letters + the "Admin" word..I dont want that two files to be outputed ,because i only want to work with visible files.


    I hope i explained my problem in an understandable way..


    So Apache freaks please be gentle and help me ..because I need this to be done
     
    narek, Jul 5, 2011 IP
  2. MartinPrestovic

    MartinPrestovic Peon

    Messages:
    213
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'll try to be gentle... :)

    First off, don't say you have written a function that you clearly got from the internet and modified. If you wrote it all yourself you would understand how it works and wouldn't need anyones help.

    Secondly, the reason why it may show you 5 files in a directory from the script but only 3 when you view it yourself is because of hidden files and file privs. If you don't want those files to be returned then you need to modify this line of the function:

    
    if ($file != "." && $file != "..") {
    
    Code (markup):
    Simply add the file names you don't want returned, e.g

    
    if ($file != "." && $file != ".." && $file != "Folder.jpg") {
    
    Code (markup):
    Lastly the error you get on certain directories... there could be a number of reasons for this but unfortunately the error message is being output in a language/gibberish which I can't understand. Usually errors with the opendir() function are either permissions related or because the path is wrong. I would lean more toward it being a permissions issue given that you are on a windows system.

    Oh and you'd have been better off putting this in the PHP forum rather than the Apache forum.
     
    MartinPrestovic, Jul 6, 2011 IP