简体   繁体   中英

Using PHP to create and save XML in subfolder

I'm using PHP to create and save xml files using this syntax:

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

It worked fine until i realized that i have more than 300 sources. 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. 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.

So i added this before adding the nodes and child for xml doc:

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

With that, i save my xml using this:

$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. so how do i insert the chmod during the file saving?

I tried: $xml->save($file(chmod($file,0777))) or 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. So you need to check the existence of the directory and create it before you call save() :

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

After this you can call $xml->save($file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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