Page 1 of 1

saving in server instead of local

Posted: Mon Jun 22, 2009 9:07 pm
by ananda
I have a savedata.php files which is used to save and load xml data from the server.

The code is:

Code: Select all

 
<php
 
$initFile = "init.xml";
$progressFile = "progress.xml";
 
$command = "load";
if(isset($_GET["command"]){
    $command = $_GET["command"];
}
 
switch($_GET["command"]){
    case "save":
        if (isset($_POST['xml'])) {
            if(write($_POST['xml'])){
                output("", "Synchronisation succeeded.");
            } else {
                output("", "Could not write file.");
            }
        } else {
            output("", "Could not find XML for output.");
        }
    break;
    case "load":
        if(!file_exists($progressFile))}
            if (!copy($initFile, $progressFile)) {
                output("", "Could not find XML for output.");
            }
        }
        output("Content-Type: text/xml", read($progressFile);
    break;
}
 
function output($headers="", $output){
    if($headers=""){
        $headers = "Content-Type: text/plain";
    }
    header ($headers);
    echo ($output);
}
 
function write($xml, $file){
    if($f = fopen($file, "w")){
        fwrite($f, $xml);
        fclose($f);
        return true;
    } else {
        return false;
    }
}
 
function read($file){
    return file_get_contents($file);
}
 
?>
 
The problem with this code is that everytime I want to save a new data, it can only save it in my local machine instead of the server.

Can anyone help me please?

Re: saving in server instead of local

Posted: Tue Jun 23, 2009 3:51 pm
by patrickmvi
What is the mechanism that you want to use to send the XML to this remote server? Would it be via HTTP POST? Or would you be FTPing an XML file to somewhere? Please give more details.

Re: saving in server instead of local

Posted: Tue Jun 23, 2009 6:18 pm
by ananda
Hi Patrick and thanks for replying,

to be honest we are still looking what the best way is. But it could be either HTTP or through FTP since we also need to pull and push data automatically to the server.

If I wanted to go with the HTTP, what sort of PHP script should be use?

Re: saving in server instead of local

Posted: Tue Jun 23, 2009 7:15 pm
by McInfo
Some problems with the script:

1. Line 12 should be switching on $command, not $_GET['command'].

2. As defined on line 42, write() requires two parameters. Yet on line 15, write() is called with only one argument. This call to write() will always fail and the file will not be saved to the server.

3. Line 35 assigns an empty string to $headers rather than comparing $headers to an empty string. The condition on line 35 will always be true.

Edit: This post was recovered from search engine cache.

Re: saving in server instead of local

Posted: Tue Jun 23, 2009 7:25 pm
by ananda
Patrick,

what other arguments should I put in line 15?

Re: saving in server instead of local

Posted: Tue Jun 23, 2009 7:50 pm
by McInfo
I'm not Patrick, but I will answer.

Look at the definition for write() on line 42. The second argument for write() should be a string containing the path to a file where you want to save the XML. It is the same as the $filename parameter of the fopen() function.

Code: Select all

write($_POST['xml'], 'example.xml')
Edit: This post was recovered from search engine cache.

Re: saving in server instead of local

Posted: Tue Jun 23, 2009 8:37 pm
by ananda
Thanks McInfo,

I think Ive been looking at the wrong PHP code, the right one is this code:

Code: Select all

 
<?php
$initFile = "init.xml";
$progressFile = "progress.xml";
 
if (isset($_POST['xml'])) { // SAVE
    if(write($_POST['xml'], $progressFile)){
        output("", "Synchronisation succeeded.");
    } else {
        output("", "Could not synchronise - write error.");
    }
} else { // LOAD
    if(!file_exists($progressFile)){
        if (!copy($initFile, $progressFile)) {
            output("", "Could not find XML for output.");
        }
        chmod($progressFile, 0755);
    }
    output("Content-Type: text/xml", read($progressFile));
}
 
function output($headers="", $output){
    if($headers == ""){
        $headers = "Content-Type: text/plain";
    }
    header ($headers);
    echo ($output);
}
 
function write($xml, $file){
    $f = fopen($file, "w") or die('Could not open file for writing.');
    fwrite($f, html_entity_decode($xml));
    fclose($f);
    return true;
}
 
function read($file){
    return file_get_contents($file);
}
 
?>
 
This code does save the file which in this case "progress.xml" file to the server, however it also need to generate a new file after a "game" ends and at the moment it is just overwriting the current "progress.xml"... Do you have any script that could do this?
And is there an HTTP path script where it could also save the "progress.xml" file to the webserver too?

Thanks

Re: saving in server instead of local

Posted: Tue Jun 23, 2009 9:38 pm
by McInfo
I'm not sure exactly what rules you want your script to follow, but if you just want to save the XML to a uniquely named file each time the script runs, you can use time() to generate a file name like "progress_1245810823.xml".

Code: Select all

$progressFile = 'progress_'.time().'.xml';
Edit: This post was recovered from search engine cache.

Re: saving in server instead of local

Posted: Tue Jun 23, 2009 10:33 pm
by patrickmvi
If you're going to want to post it to a location, you should use the CURL library. Below is a quick example of how that would be accomplished:

Code: Select all

 
$xml  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
$xml .= "<sampleRequest>\r\n";
$xml .= "   <sampleThing>123</sampleThing>\r\n";
$xml .= "</sampleRequest>";
 
$headers[] = "Content-Type: text/xml; charset=utf-8";
$headers[] = "Content-Length: " . strlen($xml);
 
$ch = curl_init("http://www.testlocation.com/api_request.php");
 
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
$resp = curl_exec($ch);
 
The resulting $resp variable would contain the response from the server where you posted the XML to.