saving in server instead of local

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ananda
Forum Newbie
Posts: 6
Joined: Mon Jun 22, 2009 9:00 pm

saving in server instead of local

Post 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?
Last edited by Benjamin on Tue Jun 23, 2009 11:40 pm, edited 1 time in total.
Reason: Changed code type from text to php.
patrickmvi
Forum Commoner
Posts: 32
Joined: Mon Jun 22, 2009 6:45 am
Location: Fort Lauderdale, FL

Re: saving in server instead of local

Post 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.
ananda
Forum Newbie
Posts: 6
Joined: Mon Jun 22, 2009 9:00 pm

Re: saving in server instead of local

Post 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?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: saving in server instead of local

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 11:37 am, edited 1 time in total.
ananda
Forum Newbie
Posts: 6
Joined: Mon Jun 22, 2009 9:00 pm

Re: saving in server instead of local

Post by ananda »

Patrick,

what other arguments should I put in line 15?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: saving in server instead of local

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 11:37 am, edited 1 time in total.
ananda
Forum Newbie
Posts: 6
Joined: Mon Jun 22, 2009 9:00 pm

Re: saving in server instead of local

Post 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
Last edited by Benjamin on Tue Jun 23, 2009 11:40 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: saving in server instead of local

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 11:38 am, edited 1 time in total.
patrickmvi
Forum Commoner
Posts: 32
Joined: Mon Jun 22, 2009 6:45 am
Location: Fort Lauderdale, FL

Re: saving in server instead of local

Post 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.
Last edited by Benjamin on Tue Jun 23, 2009 11:40 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply