简体   繁体   English

使用PHP在子文件夹中创建和保存XML

[英]Using PHP to create and save XML in subfolder

I'm using PHP to create and save xml files using this syntax: 我正在使用PHP使用以下语法创建和保存xml文件:

$xml->save("source1.xml") or die("Error");

It worked fine until i realized that i have more than 300 sources. 它工作正常,直到我意识到我有超过300个来源。 Meaning in my folder where the working files eg; 在我的文件夹中的含义工作文件例如; my css and php files i would have additional 300++ xml files mixed together with my main working files. 我的css和php文件我将额外的300 ++ xml文件与我的主要工作文件混合在一起。 On top of that, instead of hardcoding "source1.xml" i made it dynamic based on the selected source and create the xml file dynamically if, say, the selected source is "source189.xml" and that particular file doesn't exist. 最重要的是,我没有硬编码“source1.xml”,而是根据所选源动态创建动态,并动态创建xml文件,例如,所选源是“source189.xml”并且该特定文件不存在。

So i added this before adding the nodes and child for xml doc: 所以我在为xml doc添加节点和子节点之前添加了这个:

$path = 'xml_'.$level.'_syll/';
$filename = $src.'.xml';
$file = $path.$filename;

With that, i save my xml using this: 有了这个,我用这个保存我的xml:

$xml->save($file) or die("Error");

and got this error: 并得到此错误:

Warning: DOMDocument::save() [domdocument.save]: Unable to access xml_data_syll/source3.xml in file.php on line 85

Warning: DOMDocument::save(xml_data_syll/source3.xml) [domdocument.save]: failed to open stream: No such file or directory in file.php on line 85
Error

So i guessed it has something to do with permission. 所以我猜这与许可有关。 I googled and found a solution using chmod to grant the permission. 我用Google搜索并找到了使用chmod授予权限的解决方案。 so how do i insert the chmod during the file saving? 那么如何在文件保存期间插入chmod?

I tried: $xml->save($file(chmod($file,0777))) or die("Error"); 我试过:$ xml-> save($ file(chmod($ file,0777)))或die(“Error”);

and the error i got is: 我得到的错误是:

Fatal error: Call to undefined function xml_data_syll/source3.xml() in file.php on line 86

Any pointers is much appreciated. 任何指针都非常感谢。

DOMDocument::save() doesn't create intermediate directories if they don't exist. DOMDocument::save()如果不存在则不会创建中间目录。 So you need to check the existence of the directory and create it before you call save() : 因此,在调用save()之前,需要检查目录是否存在并创建它:

if (!is_dir($path)) {
    mkdir($path, 0700);
}

After this you can call $xml->save($file) 在此之后你可以调用$xml->save($file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM