Author Topic: Loading array from file within a class (beginner)  (Read 232 times)

green_as

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Loading array from file within a class (beginner)
« on: August 27, 2010, 09:09:01 AM »
Hi Experts!

I'm a sound engineer. I'm using an open source file repository - Sonuku dump - so clients can retrieve audio files from me.  Despite being a newb with php, I've made some good small hacks, including email notification when a client downloads a file - very useful for me to know!. 

I'm now trying to add a bit of back end to this script, so I can add private folders and users at easily.  All going well so far but I'm totally stuck with trying to load an array from a file, within a class. 

Originally, the array is declared as a constant at the top of the class:

PHP Code: [Select]

var $CFG_PRIVATE = array(
    
"folder1/",
    
"folder2/"                    
); //hides these folders unless logged in


Later on, this array is used like thus:

PHP Code: [Select]

foreach($this->CFG_PRIVATE as $private) {
    if (
preg_match("/^".preg_quote($private'/')."/i",$this->DIR."/") &&  !$this->checkpass($_SESSION['DUMP_PASS'])) {
      
$foundmatch true;
}



With another (separate) array for users, I successfully got it to load it's data from a csv file with this code:


PHP Code: [Select]

$dump
->DUMP_PASS = array();
$arrLines file('/path/to/users.txt');
foreach(
$arrLines as $line) {
    
$dump->DUMP_PASS explode','$line);
}                    


Can anybody advise me how I construct a function to load the array from a file, such that the array can be used as it is being used in code snippet #2 ??  I hope I have explained that clearly, any help would be massively appreciated.

thanks...
green_as

green_as

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Loading array from file within a class (beginner)
« Reply #1 on: August 27, 2010, 10:14:54 PM »
well, I sorted it out myself, took me for ever though, even though it is so basic!!   :o


PHP Code: [Select]

function readfolders() {
   
$handle fopen("/path/to/folders.csv""r");
    if( 
$handle ) {
        while ((
$data fgetcsv($handle1000",")) !== FALSE) {
            return  
$data;
        }
        
fclose($handle);
     }
}



Then I call it like this, and get the data into my array correctly

PHP Code: [Select]

$this
->CFG_PRIVATE=$this->readfolders();

« Last Edit: August 27, 2010, 10:16:35 PM by green_as »