Thread: help on fopen
View Single Post

  #1 (permalink)  
Old 05-07-2008
Joop
 
Posts: n/a
Default help on fopen

Hi all,

I'm trying to build a cms which writes, copies and moves real files
instead of writing content to a database. To remember the order in
which the pages are supposed to appear on the actual website I'm
storing a config file in each map the cms creates.

It's quite essential that te config file is being updated everytime a
page is moved from one map to another, so I have written this
function:

function set_permissions($source, $target) {
if($source["file"]) {
$directory = $source["dir"];
} else {
$directory = dirname($source["dir"]);
}

if(is_file($directory."/config.map")) {
if(is_file($target["dir"]."/config.map")) {
if($handle = @fopen($directory."/config.map","r")) {
//Bestand verwijderen uit bron-config.
$contents = fread($handle, filesize($directory."/config.map"));
fclose($handle);

if($source["file"]) {
$file = $source["file"];
} else {
$file = basename($source["dir"]);
}

$contents = explode("\r\n",$contents);
$new_source = array();

foreach($contents as $content) {
$cont = explode(":",$content);
if($cont[0] != $file) {
array_push($new_source, $content);
}
}
$new_source = implode("\r\n",$new_source);

/*if($handle = @fopen($directory."/config.map","w")) {
fwrite($handle, $new_source);
fclose($handle);
} else {
$new_source = false;
} */
}
if($handle = @fopen($target["dir"]."/config.map","r")) {
//Bestand toevoegen aan doel-config.
$contents = fread($handle, filesize($target["dir"]."/
config.map"));
fclose($handle);

if($target["file"]) {
$after = $target["file"];
} else {
$after = "index";
}

$contents = explode("\r\n",$contents);
$new_target = array();

foreach($contents as $content) {
$cont = explode(":",$content);
if($cont[0] == $after) {
array_push($new_target, $content);
array_push($new_target, $file.":".$source["permissions"]);
} else {
array_push($new_target, $content);
}
}
$new_target = implode("\r\n",$new_target);
echo $new_target."<br /><br />\n";
/* if($handle = @fopen($target["dir"]."/config.map","w")) {
fwrite($handle, $new_target);
fclose($handle);
} else {
$new_target = false;
} */
}
if($new_source && $new_target) {
$permissions = $new_source."<br /><br />\n".$new_target."<br /
><br />\n";

return $permissions;
}
}
}
return false;
}

it seems to work fine, but when I uncomment the parts that are
supposed to actually update the config file it seems te be repeating
itself... Anyone know why?!?

example config.map:
-----------------------------
index:file1.html;
file1.html:user1;
file2.html:user1;user2;user3;
file3.html:user1;user3;
map1:user1;user3;
map2:user2;
Reply With Quote