How to write a string to a server

What’s a good and simple way to write a string to a file on a server? I know it is somewhat server dependent, but I’d like to know how others do it before I dive into server side considerations. (I have access to both AWS and a more traditional server).

Thank you.

Server.printf()

Whatever Server is…

Joke aside there is an ftp library if your server offers the ftp service

FTP doesn’t lend itself well to appending to an existing file.

I’m thinking that I’ll need to send the data as the body or data of an https transaction, probably as JSON, and in the server end set things up to take the data and append it.

Right if you are not creating the file with the string then ftp might not be ideal (although there is the append command)

A simple PHP on the server could do the job if you have an http service

What’s available on the server ?

This. Let server side take care of the file, all the Arduino need do is send it to your API (AKA a web page you hit with a GET).

1 Like

If I use AWS, a lot is available but it's also a lot more work to setup. Unless they've made it easier.

If I use my DreamHost account, I have PHP 7.4 FastCGI, and more.

Plenty of options then

Constraints are what usually help narrow the choices. What else do you need to do?

For now, this works pretty well. I just have to make sure the the log file on the server is writable. Here's PHP code for the next person doing this:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php
        $username = $_GET['username'];
        $somecontent = $_GET['string'];
        $date = date(DATE_ATOM);
        $useragent = $_SERVER['HTTP_USER_AGENT'];
        $allcontent = $date."\t".$username."\t".$somecontent."\t".$useragent.PHP_EOL;

        echo "Hello, $username!<p>";

        $filename = 'test-results.txt';

        if (is_writable($filename)) {

                if (!$handle = fopen($filename, 'a')) {
                         echo "Cannot open file ($filename)";
                         exit;
                }

                if (fwrite($handle, $allcontent) === FALSE) {
                        echo "Cannot write to file ($filename)";
                        exit;
                }

                echo "Success, wrote ($allcontent) to file ($filename)";

                fclose($handle);

        } else {
                echo "The file $filename is not writable";
        }
        echo "<p>";
//     phpinfo(); 
 ?>
 </body>
</html>

Put that in a file called test.php, put it at the top level of the web server. And then go to a browser and use this URL:

http://your_server.com/test.php?username=my_name&string=string-that-is-logged&notstring=string-that-is-not-logged

Results should look like this:

Hello, my-name!

Success, wrote (2022-09-29T19:48:24-07:00 my_name string-that-is-logged Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15 ) to file (test-results.txt)

Note: There's no safety/security considerations here. Malicious actors could write long strings or malformed strings, and could cause problems for your server, take it over, and wreak havoc.

1 Like

Thanks for sharing back
This can help someone in the future

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.