Jump to content

List all files in dir, then put content of files into array


mikk809h

Recommended Posts

Hello

 

 

I have a simple question about file handling...

 

 

Is it possible to list all files in directories / subdirectories, and then read ALL files in those dirs,

and put the content of their file into an array?

 

Like this:

 

array:
[SomePath/test.php] = "All In this php file is being read by a new smart function!";
[SomePath/Weird/hello.txt = "Hello world. This is me and im just trying to get some help!";
and so on, until no further files exists in that rootdir.

 

All my attempts went totally crazy and none of them works... therefore i need to ask you for help.

 

 

Do you have any ideas how to do this?

If so, how can I be able to do it?

 

 

Thanks in Advance, pros

Heres my code...

 

Read comments on right side.

 

 

<?php
  $mysql_host = "abc123.com";
  $mysql_database = "abc123";
  $mysql_user = "abc123";
  $mysql_password = "abc123";
  $postedAuth = hash(sha256, $_POST["auth"]);
  $postedMeth = $_POST["meth"];
  $postedFile = stripslashes($_POST["data"]);
  $postedFileName = stripslashes($_POST["name"]);
  $postedID = $_POST["id"];
  $uploadsPath = "uploads/";
  $pass = hash(sha256, "somethingsecret");
  $AllFiles = array();                              #Nothing here is important... Scroll down...
  #if($postedAuth==hash(sha256, $pass)) {
    $query_1 = "SELECT * FROM Sessions";
    $sql = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database) or die("Mysql Error! cannot connect!");
    mysql_select_db($mysql_database);
    $qry_1 = mysql_query($query_1);
    $qry = mysql_fetch_array($qry_1);
    $SessionCount = $qry["SessionID"];
    if(strpos($postedMeth, "startSession")) {
      /*$Sessions = array();       
      //get all files in specified directory
      $files = glob($uploadsPath . "*");
      //print each file name
      foreach($files as $file)
      {
        //check to see if the file is a folder/directory
        if(is_dir($file))
        {
          if($file!="." && $file!="..") {
            $Sessions[] = $file;
          }
        }
      }
      $SessionCount = strval( sizeof( $Sessions ) + 1 ) . "/";
      */
      #echo $SessionCount;
      $SessionCount ++;
      #echo "Incrementing count to: " . $SessionCount;
      mysql_query("UPDATE Sessions SET SessionID=" . $SessionCount) or die("Mysql: Cannot update sessionID!");
      $qry_2 = mysql_query("SELECT SessionID FROM Sessions") or die("Cannot select table");
      $qry_3 = mysql_fetch_array($qry_2);
      $SessionCount = $qry_3["SessionID"] . "/";
      #echo "CREATING DIRS... : " . $uploadsPath . $SessionCount;
      mkdir( $uploadsPath . $SessionCount ) or die("Unable to create path!");
      mkdir( $uploadsPath . $SessionCount . "System" );
      mkdir( $uploadsPath . $SessionCount . "System/Apis" );
      mkdir( $uploadsPath . $SessionCount . "System/Apps" );
      #echo " SessionCount: " . $SessionCount;
    } elseif(strpos($postedMeth, "inSession")) {
      #echo "Putting file into: " . $uploadsPath . $SessionCount . "/" . $postedFileName;
      $filee = fopen( $uploadsPath . $SessionCount . "/" . $postedFileName, "w" ) or die("FAILED TO OPEN FILE!");
      fwrite( $filee, $postedFile );
      fclose( $filee );
      #echo "Put file!";
    } elseif(strpos($postedMeth, "getSession")) {                    #Here it is....
      if($SessionCount>=1) {
        if(strpos($postedID, "latest")) {
          $rootpath = $uploadsPath . $SessionCount;                   #Most of the code is commented, because it didnt work out as i wanted...
          $AllFiles = glob($rootpath . "*");                          #And yes.. I changed this name, because i found another example on web, which
                                                                      #Didn't work either... It should just be $fileinfos = scandir( $rootpath . "*" );
          /*foreach($fileinfos as $pathname => $fileinfo) {
            if (!$fileinfo->isFile()) continue;
            $realPath = $uploadsPath . $SessionCount . "/" . $pathname;
            $filee = fopen( $realPath, "r" ) or die("FAILED TO READ FILE!");
            $content = fread($filee,filesize($realPath));
            fclose($filee);
            $AllFiles[$pathname] = $content;
            var_dump($pathname);
          }*/
        } else{
          $rootpath = $uploadsPath . $postedID;
          $AllFiles = glob($rootpath . "*");
          /*foreach($fileinfos as $pathname => $fileinfo) {
            if (!$fileinfo->isFile()) continue;
            $realPath = $uploadsPath . $postedID . "/" . $pathname;
            $filee = fopen( $realPath, "r" ) or die("FAILED TO READ FILE!");
            $content = fread($filee,filesize($realPath));
            fclose($filee);
            $AllFiles[$pathname] = $content;
            var_dump($pathname);
          }*/

        }
        echo($AllFiles);
      }
    }
  #} else{
  #  echo "Invalid Access";
  #}
?>


 

 

Thats my code... ( of course without passwords and all that stuffz.. )

 

 

I have no idea why this part doesnt work as i wanted.

 

          foreach($fileinfos as $pathname => $fileinfo) {
            if (!$fileinfo->isFile()) continue;
            $realPath = $uploadsPath . $SessionCount . "/" . $pathname;
            $filee = fopen( $realPath, "r" ) or die("FAILED TO READ FILE!");
            $content = fread($filee,filesize($realPath));
            fclose($filee);
            $AllFiles[$pathname] = $content; #This was to put the filepath ( full path ) and the content of the file into the array..
            var_dump($pathname);          # But it didnt seem to work.
          }
I hope this helps a bit

 

 

Thanks

EDIT:

 

I actually found the solution to the file listening...

 

But i dont think it works very well...

For example I have a file called test.lua in System/Apis/test.lua

The array noticed that as [uploads/1] => "Uploads/1";

 

 

Why?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.