I have one script, let's call it "main" php script, there is another script, call it "writer". And I have a xml file. My "writer" get's info from db and writes it to the xml. All work fine if I execute my writer directly.
Right now I want to execute "writer" from my "main" script. The problem is, that I can see my output from writer file in my main (I add some echo's for testing purposes), but it doesn't rewrite my xml file like it does when I execute it directly.
How can I solve this problem?
Thanks.
Code from main:
<?php
session_start();
include_once("./../writer.php");
$_SESSION['make_sitemap'] = true;
echo 'ok';
?>
Code from writer:
<?php
session_start();
// include some utility files and header, also connection to db
if($_SESSION['make_sitemap']){
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
// some boring stuff with xml variable
$fp = fopen('./myxml.xml', 'w');
fwrite($fp, $xml, strlen($xml));
fclose($fp);
echo 'done';
}
?>
Thats all folks, nothing extraordinary, I think. But hmm why it doesn't rewrite, I don't know.
Relative Paths in php are resolved based where they are executed, meaning where ever the file that php originally starting running the request with. This means that when you hit your writer.php
directly it means that ./myxml.xml
will appear in the same directory that you writer.php lives in likewise with main.php.
A better bet would be to establish a common place that all paths would be relative to like $_SERVER["DOCUMENT_ROOT"]
.
so instead of
./myxml.xml
you would say
$_SERVER["DOCUMENT_ROOT"]."/path/to/my/myxml.xml"
If this becomes too tedious you can define a couple of known locations in a common include like.
define("XML_PATH", $_SERVER["DOCUMENT_ROOT"]."/path/to/my");
then use it as
XML_SAVE_PATH."/myxml.xml"