Php leest directory => schrijft naar xml
Ik zoek een scriptje waarbij de directory wordt gelezen door php ,
dat je in het begin kan kiezen wat er wordt geschreven (bv. )
dan een hoofdelement zoals <header> (al de namen worden in het begin gekozen )
en dan de verschillende extensies (ergens in het begin gekozen dewelke)
worden opgeslagen als een subelement bv <element><item>iets</item></element>
als er dan een txt-bestand dezelde naam heeft als het bestand met de toegelaten extensie , deze er tussenkomt zoals: <element><item>iets</item><explenation>wat er in het txt bestand staat</explenation></element>
Dus moet het eindresultaat er zo uitzien:
(met inspringen en returns als het kan)
<dir>
<header>
<item>1.html</item>
</header>
<header>
<item>2.html</item>
<explenation>Dit is wat er in staat</explenation>
</header>
</dir>
Zoek even een script op van webmakerij. Het heet "site-map .. ..", weet niet precies wat er op die puntjes moet. Dat script doet precies wat je wilt.
deze doet het grootste deel van het werk !
enkel , als ik een map in de map waar hij de bestanden moet 'herkennen', krijg ik een foutmelding :( + ik heb geen idee hoe ik die extensies gedaan moet krijgen .
Hier is het script tot nu toe:
index.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$path = "Images"; //Path for files
if ($path != ""){
$path2 =$path . "/"; }
$xml_file = $path2 . "Images.xml"; //Name for xml
include($path2 . "1.php");
$Obj = new DirectoryToXml($path);
$Obj->traverseDirFiles($path);
$Obj->setArray($Obj->arr);
if ($Obj->saveArray($xml_file)){
echo "<p><b>Errossr! $xml_file </b></p>";
}else{
echo "<p><b>Error! xml_file</b></p>";
}
?>
$path = "Images"; //Path for files
if ($path != ""){
$path2 =$path . "/"; }
$xml_file = $path2 . "Images.xml"; //Name for xml
include($path2 . "1.php");
$Obj = new DirectoryToXml($path);
$Obj->traverseDirFiles($path);
$Obj->setArray($Obj->arr);
if ($Obj->saveArray($xml_file)){
echo "<p><b>Errossr! $xml_file </b></p>";
}else{
echo "<p><b>Error! xml_file</b></p>";
}
?>
1.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
class DirectoryToXml extends CArray2xml2array
{
public $arr;
private $mainString;
private $path;
public function __construct($path)
{
$this->arr = array();
$this->mainString = "";
$this->path = $path;
}
public function traverseDirFiles($path)
{
$dir = opendir ($path);
while ($file = readdir ($dir))
{
if (($file == ".") or ($file == ".."))
{
continue;
}
if (filetype("$path/$file") == "dir")
{
$this->traverseDirFiles("$path/$file");
}
else
{
$this->processFiles($path,$file);
}
}
closedir($dir);
}
function processFiles($path, $file)
{
if(trim($path)==trim($this->path))
{
$arr_to_form = 'this->arr[Image]';
}
else
{
$dat = $this->formArray($this->path.$path);
$arr_to_form = 'this->arr[Image]'.$dat;
}
$searches = array("[","]");
$reps = array("['", "']");
$arr_2 = str_replace($searches, $reps, trim($arr_to_form));
$vr = $arr_2.'[]';
eval("\$$vr=\"$file\";");
}
public function showDirHierarchy()
{
$this->dBug($this->arr);
}
function dBug($dump)
{
echo "<pre>";
print_r($dump);
echo "</pre>";
}
function formArray($str)
{
$arr = explode("$this->path/",$str);
$arr = explode("/",$arr[1]);
foreach($arr as $i=>$v)
{
$arr_to_form .= "[$v]";
}
return $arr_to_form;
}
}
class CArray2xml2array
{
private $XMLArray;
private $arrayOK;
private $XMLFile;
private $fileOK;
private $doc;
public function __construct(){
}
public function setXMLFile($XMLFile){
if (file_exists($XMLFile)){
$this->XMLFile = $XMLFile;
$this->fileOK = true;
}else{
$this->fileOK = false;
}
return $this->fileOK;
}
public function saveArray($XMLFile, $rootName="", $encoding="utf-8"){
global $debug;
$this->doc = new domdocument("1.0", $encoding);
$arr = array();
if (count($this->XMLArray) > 1){
if ($rootName != ""){
$root = $this->doc->createElement($rootName);
}else{
$root = $this->doc->createElement("root");
$rootName = "root";
}
$arr = $this->XMLArray;
}else{
$key = key($this->XMLArray);
$val = $this->XMLArray[$key];
if (!is_int($key)){
$root = $this->doc->createElement($key);
$rootName = $key;
}else{
if ($rootName != ""){
$root = $this->doc->createElement($rootName);
}else{
$root = $this->doc->createElement("root");
$rootName = "root";
}
}
$arr = $this->XMLArray[$key];
}
$root = $this->doc->appendchild($root);
$this->addArray($arr, $root, $rootName);
if ($this->doc->save($XMLFile) == 0){
return false;
}else{
return true;
}
}
function addArray($arr, &$n){
foreach ($arr as $key => $val){
if (is_int($key)){
if (strlen($name)>1){
$newKey = $name;
}else{
$newKey="item";
}
}else{
$newKey = $key;
}
$node = $this->doc->createElement($newKey);
if (is_array($val)){
$this->addArray($arr[$key], $node, $key);
}else{
$nodeText = $this->doc->createTextNode($val);
$node->appendChild($nodeText);
}
$n->appendChild($node);
}
}
public function setArray($XMLArray){
if (is_array($XMLArray) && count($XMLArray) != 0){
$this->XMLArray = $XMLArray;
$this->arrayOK = true;
}else{
$this->arrayOK = false;
}
return $this->arrayOK;
}
}
?>
class DirectoryToXml extends CArray2xml2array
{
public $arr;
private $mainString;
private $path;
public function __construct($path)
{
$this->arr = array();
$this->mainString = "";
$this->path = $path;
}
public function traverseDirFiles($path)
{
$dir = opendir ($path);
while ($file = readdir ($dir))
{
if (($file == ".") or ($file == ".."))
{
continue;
}
if (filetype("$path/$file") == "dir")
{
$this->traverseDirFiles("$path/$file");
}
else
{
$this->processFiles($path,$file);
}
}
closedir($dir);
}
function processFiles($path, $file)
{
if(trim($path)==trim($this->path))
{
$arr_to_form = 'this->arr[Image]';
}
else
{
$dat = $this->formArray($this->path.$path);
$arr_to_form = 'this->arr[Image]'.$dat;
}
$searches = array("[","]");
$reps = array("['", "']");
$arr_2 = str_replace($searches, $reps, trim($arr_to_form));
$vr = $arr_2.'[]';
eval("\$$vr=\"$file\";");
}
public function showDirHierarchy()
{
$this->dBug($this->arr);
}
function dBug($dump)
{
echo "<pre>";
print_r($dump);
echo "</pre>";
}
function formArray($str)
{
$arr = explode("$this->path/",$str);
$arr = explode("/",$arr[1]);
foreach($arr as $i=>$v)
{
$arr_to_form .= "[$v]";
}
return $arr_to_form;
}
}
class CArray2xml2array
{
private $XMLArray;
private $arrayOK;
private $XMLFile;
private $fileOK;
private $doc;
public function __construct(){
}
public function setXMLFile($XMLFile){
if (file_exists($XMLFile)){
$this->XMLFile = $XMLFile;
$this->fileOK = true;
}else{
$this->fileOK = false;
}
return $this->fileOK;
}
public function saveArray($XMLFile, $rootName="", $encoding="utf-8"){
global $debug;
$this->doc = new domdocument("1.0", $encoding);
$arr = array();
if (count($this->XMLArray) > 1){
if ($rootName != ""){
$root = $this->doc->createElement($rootName);
}else{
$root = $this->doc->createElement("root");
$rootName = "root";
}
$arr = $this->XMLArray;
}else{
$key = key($this->XMLArray);
$val = $this->XMLArray[$key];
if (!is_int($key)){
$root = $this->doc->createElement($key);
$rootName = $key;
}else{
if ($rootName != ""){
$root = $this->doc->createElement($rootName);
}else{
$root = $this->doc->createElement("root");
$rootName = "root";
}
}
$arr = $this->XMLArray[$key];
}
$root = $this->doc->appendchild($root);
$this->addArray($arr, $root, $rootName);
if ($this->doc->save($XMLFile) == 0){
return false;
}else{
return true;
}
}
function addArray($arr, &$n){
foreach ($arr as $key => $val){
if (is_int($key)){
if (strlen($name)>1){
$newKey = $name;
}else{
$newKey="item";
}
}else{
$newKey = $key;
}
$node = $this->doc->createElement($newKey);
if (is_array($val)){
$this->addArray($arr[$key], $node, $key);
}else{
$nodeText = $this->doc->createTextNode($val);
$node->appendChild($nodeText);
}
$n->appendChild($node);
}
}
public function setArray($XMLArray){
if (is_array($XMLArray) && count($XMLArray) != 0){
$this->XMLArray = $XMLArray;
$this->arrayOK = true;
}else{
$this->arrayOK = false;
}
return $this->arrayOK;
}
}
?>
ip schreef op 31.12.2007 17:29:
... krijg ik een foutmelding :( ...
Welke?
Maar de indeling klopt niet: het is :
- <Image>
<item>1.php</item>
- <item>
<item>3.gif</item>
<item>4.gif</item>
</item>
<item>Images.xml</item>
</Image>
en het zou moeten zijn:
- <Image>
<item>1.php</item>
<item>Images/3.gif</item>
<item>Images/4.gif</item>
<item>Images.xml</item>
</Image>
uiteindelijk dan zonder het phpbestand en xmlbestand.
Gewijzigd op 01/01/1970 01:00:00 door ip