lessto CloudToFile

Demo of WordPress PHP template, where data is transferred to and from a FTP site. A simple cloud to file function.

Code for Business Central 365 Spring Edition (14)
Code for Business Central 365 Wave 2 (15)

If you take all the WP stuff out, the code works in plain PHP.

Feel free to use the code if you have projects with http request, JSON conversion and BASE64.

The keypoints is to change the APIKEY and ftp information, this can be done in the code. In customer code it should be saved in a table.

Here is the code.

The working demo where this code comes from is here.

WordPress PHP cloud to file demo

<?php
/**
 * Template Name: lessto - cloud - ftp - com
 */
get_header();
$website = 'https://lessto.dk/cloudtofile/lessto-ftp-connect';
$apikey = '<Your API key from lessto Cloud To File>';
$user = '<FTP server user name>';
$pass = '<FTP server user password>';
$ftpserver = '<FTP server name>';
$ftpdir = '<FTP server directory>';
?>
<html>
<style>
.spacemobile {
  display: none;
}
@media (max-width: 800px) {
  .spacemobile {
    display: block;
  }
}
</style>
<body>
<br>
<div class="spacemobile"><br><br></div>
<span style="font-family:Impact, Charcoal, sans-serif;font-size:40px;color:rgba(54, 162, 235, 0.8);">lessto CloudToFile</span>
 <br>
 <h2>Demo of website sending and recieving FTP files.</h2>
<p>It uses the four simple commands in lessto CloudToFile</p>
<br>
<form method = "POST">
<input type = "submit" name = "action" class="less-submit" value = 'list'>
<input type = "submit" name = "action" class="less-submit" value = 'send'>
<input type = "submit" name = "action" class="less-submit" value = 'get'>
<input type = "submit" name = "action" class="less-submit" value = 'delete'>
</form>
<?php
if ($_POST["action"] == 'list') {
    $action = 'list';
    $postdata = http_build_query(
        array(
            'apikey' => $apikey,
            'action' => $action,
            'user' => $user,
            'pass' => $pass,
            'ftpserver' => $ftpserver,
            'ftpdir' => $ftpdir)
    );
    $post = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $postdata)
    );
    $postcontext  = stream_context_create($post);
    $result = @file_get_contents($website, false, $postcontext);
    if (!$result) {
        echo '<br>';
        echo 'errorcode=70';
        echo '<br>';
        echo 'errortext=No connection to lessto CloudToFile';
        echo '<br><br><br><br><br>';
        get_footer();
        die;
    }
    $resultarray = @json_decode($result);
    if ($resultarray->errorno != '') {
        echo '<br>';
        echo 'errorcode='.$resultarray->errorno;
        echo '<br>';
        echo 'errortext='.$resultarray->errortext;
        echo '<br><br><br><br><br>';
        get_footer();
        die;
    }
    echo '<br>';
    echo 'status='.$resultarray->status;
    echo '<br>';
    echo '<br>';
    if (count($resultarray) > 0) {
        foreach ($resultarray as $key=>$value) {
            if (strpos($key, 'file') !== false) {
                $filelist = $filelist.$value;
                $filelist = $filelist.'<br>';
            }
        }
    }
    echo 'Filelist:';
    echo '<br>';
    echo '<br>';
    echo $filelist;
}
if ($_POST["action"] == 'send') {
    $action = 'send';
    $filecontent =
    '"1122332","Furniture store"<br>'.
    '"1122311","Car store"<br>'.
    '"1122322","Tree store"<br>'.
    '"1119932","Cheese store"<br>'.
    '"9482843","Bicycle store"<br>'.
    '"9922133","Gun store"';
    $base64 = base64_encode($filecontent);
    $filename = 'testfile.txt';
    $postdata = http_build_query(
        array(
            'apikey' => $apikey,
            'action' => $action,
            'user' => $user,
            'pass' => $pass,
            'ftpserver' => $ftpserver,
            'ftpdir' => $ftpdir,
            'ftpfilename' => $filename,
            'base64' => $base64)
    );
    $post = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $postdata)
    );
    $postcontext  = stream_context_create($post);
    $result = @file_get_contents($website, false, $postcontext);
    if (!$result) {
        echo '<br>';
        echo 'errorcode=70';
        echo '<br>';
        echo 'errortext=No connection to lessto CloudToFile';
        echo '<br><br><br><br><br>';
        get_footer();
        die;
    }
    $resultarray = @json_decode($result);
    if ($resultarray->errorno != '') {
        echo '<br>';
        echo 'errorcode='.$resultarray->errorno;
        echo '<br>';
        echo 'errortext='.$resultarray->errortext;
        echo '<br><br><br><br><br>';
        get_footer();
        die;
    }
    echo '<br>';
    echo 'status='.$resultarray->status;
    echo '<br>';
    echo '<br>';
    echo 'Filesend: OK';
    echo '<br>';
}
if ($_POST["action"] == 'get') {
    $action = 'get';
    $filename = 'testfile.txt';
    $postdata = http_build_query(
        array(
            'apikey' => $apikey,
            'action' => $action,
            'user' => $user,
            'pass' => $pass,
            'ftpserver' => $ftpserver,
            'ftpdir' => $ftpdir,
            'ftpfilename' => $filename)
    );
    $post = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $postdata)
    );
    $postcontext  = stream_context_create($post);
    $result = @file_get_contents($website, false, $postcontext);
    if (!$result) {
        echo '<br>';
        echo 'errorcode=70';
        echo '<br>';
        echo 'errortext=No connection to lessto CloudToFile';
        die;
    }
    $resultarray = @json_decode($result);
    $res_errorcode = $resultarray->errorno;
    $res_errortext = $resultarray->errortext;
    if ($res_errorcode != '') {
        echo '<br>';
        echo 'errorcode='.$res_errorcode;
        echo '<br>';
        echo 'errortext='.$res_errortext;
        echo '<br><br><br><br><br>';
        get_footer();
        die;
    }
    echo '<br>';
    echo 'status='.$resultarray->status;
    echo '<br>';
    echo '<br>';
    echo 'Fileget:';
    echo '<br>';
    echo '<br>';
    $filecontent = base64_decode($resultarray->base64);
    echo $filecontent;
}
if ($_POST["action"] == 'delete') {
    $action = 'delete';
    $filename = 'testfile.txt';
    $postdata = http_build_query(
        array(
            'apikey' => $apikey,
            'action' => $action,
            'user' => $user,
            'pass' => $pass,
            'ftpserver' => $ftpserver,
            'ftpdir' => $ftpdir,
            'ftpfilename' => $filename)
    );
    $post = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $postdata)
    );
    $postcontext  = stream_context_create($post);
    $result = @file_get_contents($website, false, $postcontext);
    if (!$result) {
        echo '<br>';
        echo 'errorcode=70';
        echo '<br>';
        echo 'errortext=No connection to lessto CloudToFile';
        echo '<br><br><br><br><br>';
        get_footer();
        die;
    }
    $resultarray = json_decode($result);
    if ($resultarray->errorno != '') {
        echo '<br>';
        echo 'errorcode='.$resultarray->errorno;
        echo '<br>';
        echo 'errortext='.$resultarray->errortext;
        echo '<br><br><br><br><br>';
        get_footer();
        die;
    }
    echo '<br>';
    echo 'status='.$resultarray->status;
}
get_footer();