saving, loading & setup of user data via .txt files
Posted: Tue Nov 17, 2009 2:46 pm
Hi,
I have assembled the following code that saves a specific data array to a .txt file based on the users name ('user_id_0000001.txt' will be dynamic in the final code).
I was wondering how I might tweak the code so that if a user's .txt file already exists, it will read the array saved in that file, and if a user's .txt file doesnt exist yet, then it will create the .txt file and save a default data array there.
Then, each time the user's .txt file is loaded, I would like to +1 to the variables in their array and save to their .txt file.
Its basically the bare-bones for a game where i need to be able to save the user's variables without overighting existing users with the default variables.
any help would be sorely appreciated,
many thanks,
Dave
I have assembled the following code that saves a specific data array to a .txt file based on the users name ('user_id_0000001.txt' will be dynamic in the final code).
I was wondering how I might tweak the code so that if a user's .txt file already exists, it will read the array saved in that file, and if a user's .txt file doesnt exist yet, then it will create the .txt file and save a default data array there.
Then, each time the user's .txt file is loaded, I would like to +1 to the variables in their array and save to their .txt file.
Its basically the bare-bones for a game where i need to be able to save the user's variables without overighting existing users with the default variables.
any help would be sorely appreciated,
many thanks,
Dave
Code: Select all
<?php
$filepath='user_id_0000001.txt';
//
// load a tab seperated text file as array
//
function load_tabbed_file($filepath, $load_keys=false){
$array = array();
if (!file_exists($filepath)){ return $array; }
$content = file($filepath);
for ($x=0; $x < count($content); $x++){
if (trim($content[$x]) != ''){
$line = explode("\t", trim($content[$x]));
if ($load_keys){
$key = array_shift($line);
$array[$key] = $line;
}
else { $array[] = $line; }
}
}
return $array;
}
// save an array as tab seperated text file
$array = array(
'line1' => array('data-1-1', 'data-1-2', 'data-1-3'),
'line2' => array('data-2-1', 'data-2-2', 'data-2-3'),
'line3' => array('data-3-1', 'data-3-2', 'data-3-3'),
'line4' => 'default',
'line5' => 'default2'
);
// save the array to the .txt file:
write_tabbed_file($filepath, $array, true);
function write_tabbed_file($filepath, $array, $save_keys=false){
$content = '';
reset($array);
while(list($key, $val) = each($array)){
// replace tabs in keys and values to [space]
$key = str_replace("\t", " ", $key);
$val = str_replace("\t", " ", $val);
if ($save_keys){ $content .= $key."\t"; }
// create line:
$content .= (is_array($val)) ? implode("\t", $val) : $val;
$content .= "\n";
}
if (file_exists($filepath) && !is_writeable($filepath)){
return false;
}
if ($fp = fopen($filepath, 'w+')){
fwrite($fp, $content);
fclose($fp);
}
else { return false; }
return true;
}
// load the saved array:
$reloaded_array = load_tabbed_file($filepath, true);
print_r($reloaded_array);
// returns the array from above
?>