I needed some functions to 'zip' a dir, or better unzip an inline dir from one script.
With these functions it became pretty trivial to take a directory, read all files from it and save them somewhere else.
This to make it possible to have a sort of php installer that unarchives an inline directory structure into a real dir which can be distributed as a single .php file.
1
2 $sourcedir = 'example/source';
3 $destdir = 'example/dest';
4 unpackdir(packdir($sourcedir),$destdir);
5 function packdir($td = '.') {
6 $rv = array();
7 //print "\ndoing $td";
8 foreach (scandir($td) as $tf) {
9 if ($tf != '.' && $tf != '..') {
10 if (is_dir($td.'/'.$tf)) {
11 $rv[$tf] = packdir($td.'/'.$tf);
12 } else {
13 $rv[$tf] = base64_encode(file_get_contents($td.'/'.$tf));
14 }
15 }
16 }
17 return($rv);
18 }
19 function unpackdir($filestruct,$cd = '.') {
20 foreach ($filestruct as $fn => $fv) {
21 if (is_array($fv)) {
22 createdir($cd.'/'.$fn);
23 unpackdir($fv,$cd.'/'.$fn);
24 }else{
25 unpackfile($fv,$cd,$fn);
26 }
27 }
28 }
29 function createdir($dirname) {
30 print "\nCreating $dirname.";
31 mkdir($dirname);
32 }
33 function unpackfile($fv,$cd,$fn) {
34 $fh = fopen($cd . '/' . $fn,'w+');
35 if ($fh) {
36 print_r($fh);
37 fwrite($fh,base64_decode($fv));
38 fclose($fh);
39 print "\nUnpacked $cd/$fn into $cd with ".strlen($fv)." bytes of base64.\n";
40 } else {
41 print "couldn't open $fn in write modus";
42 }
43 }
44
20110126
Subscribe to:
Posts (Atom)