Introduction: Raspberry Pi - Use PHP to Transfer Files to a Remote Server
This is a short instructable which deals with automating the transfer of files from your Raspberry Pi to a web server using a PHP script..
Why do it all I hear you ask? Well, backup is the obvious answer but you might want to send IOT information or an alert as part of a project. By having a PHP script with this functionality you can automate FTP uploads using the Debian Cron scheduler or event detection, say in conjunction with Inotify or equivalent.
What you will need
1. A Raspberry Pi running Linux - seems to work on all models
2. A remote server running Linux which has FTP on it and user credentials which you know.
3. Terminal access from a PC or other computer. (I chose Putty which I accessed from my Windows surface tablet)
4. PHP installed on your local Pi.
5. FileZilla Client - as an option.
(See the overall technical architecture in the above photo)
(HEATH WARNING - FTP servers need to be secured, particularly if you are using an internet based web server. So please ensure that you harden your environment and do not use this for sensitive data of any nature. I cannot be responsible for your misuse of this instructable)
Step 1: FTP Servers, PHP and All That
For my IOT projects, I find it useful to have my own internet based server but as a beginner you should consider using instead a local server, indeed another R Pi.. I have an internet based Debian virtual server for which I pay $4.5 dollars per month. I installed on it a very simple FTP server . This is a very useful site which shows how to install an FTP server - https://debian-administration.org/article/228/Sett... In my case, I installed proftpd on the Debian server and added an FTP user account for which I used a very strong password and locked the user down to their home directory, e.g. if the user name is bob then by files uploaded will be directed to the folder /home/bob
You need to install PHP on your local Pi. The installation process for PHP on a PI is well documented so I won't cover it here. To keep things simple, I used the Linux command line to run my FTP php script. For more advanced users, you could achieve this by running the PHP scripts from your Apache web server and execute it from a web browser.
As well as putty, it's useful to have the Filezilla client installed on your Windows device to validate testing.
Step 2: Running the PHP Script
Firstly, using Putty, log onto the Raspberry Pi and choose any directory from which you can run a PHP script. I would usually use the web root, e.g. /var/www/ or /var/www/html. You would type sudo cd/var/www/html etc
Next, create a text file which you want to send to the remote server. I chose logo.txt which I created by opening in the nano editor. The commands for this are sudo nano logo.txt into which you can add some random text.
Next, I created the php script in the same directory. I chose the name myftp.php. Again, I created this file using the nano editor. To see the PHP code, please open the above file, myftp.txt then cut and paste the code into myftp.php on your Pi. The commands are sudo nano myftp.php
You must change the owner of the files to www-data or whatever account owns he web root folder (which you are currently in) by typingsudo chown www-data *.* and then ensure the text file and php file have the correct permissions by typing sudo chmod 755 *.*
To run the PHP script, type the following
sudo php myftp.php
If successful, you will see the message successfully uploaded logo.txt
Now, open FileZilla, set up a connection to the FTP server (using the FTP user credentials you used earlier) and you should see the file logo.txt in the home folder.
Hope you enjoy this.
We have a be nice policy.
Please be positive and constructive.