This is a discussion on help on fopen within the PHP Language forums, part of the PHP Programming Forums category; Hi all, I'm trying to build a cms which writes, copies and moves real files instead of writing content ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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; |
|
|||
|
On 7 May, 12:12, Joop <jc_1...@hotmail.com> wrote:
> 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; I'm not sure exactly what you mean by "repeating itself" as you have not given an example of this. But I have to wonder why you are bothering to write this when there is limboserver out there ready to download? http://www.limboportal.com/ |
|
|||
|
Well, just like I said: "It repeats itself". The general idea is to
make a drag and drop script, to drag and drop files and maps on one and eachother. Releasing the mousebutton submits a form and the php script is set off with two arrays: $source and $taget, looking something like: $source["dir"] = ".."; $source["file"] = "file1.html"; $target["dir"] = "../map1"; $target["file"] = "file3.html"; the idea is that (in this case) the file ../file1.html is placed in the map ../map1 and in the config file appears after file3.html. The function works perfectly right now, and echos $new_target quite nicely. But when I uncomment the part that fopens, fwrites and fcloses it to config.map the config file looks something like this: index:file3.html; file1.html:1; file1.html:1; file1.html:1; file1.html:1; file3.html:user1; file1.html:1; file1.html:user1; Don't know exactly how many times, but I am trying to make it look like this: index:file3.html; file3.html:user1; file1.html:user1; And I don't know about the limboserver-thingy... It's probably a very good suggestion, but I just wanted to make my own... Thanks in advance... |
|
|||
|
Joop wrote:
> Well, just like I said: "It repeats itself". The general idea is to > make a drag and drop script, to drag and drop files and maps on one > and eachother. Releasing the mousebutton submits a form and the php > script is set off with two arrays: $source and $taget, looking > something like: > > $source["dir"] = ".."; > $source["file"] = "file1.html"; > $target["dir"] = "../map1"; > $target["file"] = "file3.html"; > > the idea is that (in this case) the file ../file1.html is placed in > the map ../map1 and in the config file appears after file3.html. > > The function works perfectly right now, and echos $new_target quite > nicely. But when I uncomment the part that fopens, fwrites and fcloses > it to config.map the config file looks something like this: > > index:file3.html; > file1.html:1; > file1.html:1; > file1.html:1; > file1.html:1; > file3.html:user1; > file1.html:1; > file1.html:user1; > > Don't know exactly how many times, but I am trying to make it look > like this: > > index:file3.html; > file3.html:user1; > file1.html:user1; > > And I don't know about the limboserver-thingy... It's probably a very > good suggestion, but I just wanted to make my own... > Thanks in advance... > How did your file start out in the first place? A couple of things. You shouldn't use "\r\n" for our line end. Just use "\n" - it's more portable. And don't explode() the input. Since you want it in an array anyway, just use file(). Must simpler and faster. Then you can either write each array element out, or use implode("\n", $contents) then write once. The former is slower, but takes less memory (which may be important if it's a large file). Also be sure to flock() your config file from before you read it until after you write it. Otherwise you can run into major problems should two people be trying to do something at the same time. Now, your problem with the duplicate entries can be traced to this: 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); } } } You are pushing the new target into the array when you test against each element of the array. I think you only want to do it once, depending on if the element exists already or not. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
![]() |
| Thread Tools | |
| Display Modes | |
|
|