Automatically post a file to a website with FTP

This automation has been a real game-changer for me. I run a bunch of websites, one for a local non-profit. They have a membership directory which resides in a protected directory that only members can access. When someone’s contact info changes, a new directory PDF is generated and I need an easy way to get that on the website, and quickly.

Today, I’m writing about that—the auto-posting part—as opposed to how I generate the membership directory PDF in FileMaker (where the organization’s membership info is stored) which auto-triggers a command-line script to run from within FileMaker (very cool). However, all of that is pretty specific to my situation … it doesn’t really matter how the PDF is formed … the important point is that I want it uploaded to replace an older file with the same name (this is important, so that you don’t have to edit links in web pages!), and no doubt you’ll be making your PDF or other file quite a different way than I am.

Things you’ll need:

  • FTP program. Apple removed the FTP program in its most recent operating systems—something about security reasons, but I don’t pretend to understand. Thankfully, I still have an old iMac running Lion, so I was able to get a copy of FTP from there. If you get the program, don’t put it in the standard location /usr/bin as it could (will?!) be removed on an operating system update. (It’s happened to me. Now I keep an extra copy in Dropbox just in case!) Instead, I put a copy in usr/local/bin and run it from there. And there are other ways to get the FTP program.
  • FTP username & password (you may need to go into your website hosting cpanel and make an FTP user). Pro tip: I create a special user for this and in the FTP path, put the directory where the file needs to go. This way, after logging in, you’ll be right where you need to be, saving you from having to cd (change directory) all over the place.
  • You need to create a plain text file called .netrc (that’s right—there’s no filename extension on it) in the root of your home directory on your local computer. (And files that begin with a dot will be hidden.)

    Now, back to .netrc … In Terminal, in the user’s root directory (get there quickly by typing cd ~ and pressing return), type:
    touch .netrc (and press return)
    chmod 700 .netrc (and press return)

    The first command creates the file, while chmod sets the permissions on the file so that only the owner can view it (crucial). The easiest, fastest way to edit the file would be with nano: type nano .netrc and then return. Then type the following three lines, replacing the fake info with your real info:
    machine ftp.yourwebsite.com
    login yourusername@yourwebsite.com
    password putyourpasswordherefortheftpuser

Press Control-X to save the file and exit Nano.

What have we done so far? We’ve set the foundation for a script to run automatically without user intervention, and the script will know to look for the .netrc file to get the login credentials.

Now, let’s write a script. You can use Nano again, or any other text editor.
nano auto_upload.sh (and return)
#!/bin/bash
/usr/local/bin/ftp -d -i ftp.yourserver.com << ftpEOF
put "yourfile.pdf"
quit
ftpEOF

Press Control-X to save the file and exit Nano.

Once saved, you'll need to make the script executable (the chmod line) and move it (mv) to /usr/local/bin (I like things in one place—less to remember).

chmod u+x auto_upload.sh (and press return)
mv auto_upload.sh /usr/local/bin (and press return)

Now, when you have your updated file ready to upload, it's just a matter of typing the following:
./usr/local/bin/auto_upload.sh (and press return)

You could even use Automator to make running that as an app on your desktop or Keyboard Maestro to assign a keyboard shortcut to it.

Extra Credit

There's another text file I generate at the same time I generate a new membership directory ... I call it postdate.txt and it has one line:
Last updated on [date]
(where [date] is replaced automatically by the current date). There are different ways that file might be auto-generated ... I use FileMaker, but I might explore some other possibilities in a future post.

So, how do I use this postdate.txt file? I want it to show on the home page of the website so that everyone knows when the membership directory was last updated, and I also want to use it on the page that has the link to the directory. For the home page, I use an "Insert PHP Snippet" widget (from the WordPress plugin, "Insert PHP code Snippet" by xyzscripts.com). By doing this, I don't have to go into the website and edit the page manually. I *love* this more than I'm comfortable admitting.

That postdate.txt file also gets stored in the root of my home directory, so I add another line to the script up above: put "postdate.txt"

Of course, in my scenario, the membership directory goes in a protected directory, and the postdate.txt file has to go into a public folder (I use the usual uploads folder of WordPress). That necessitates adding some additional lines to the script to maneuver to the right folders, but I won't get into that here.
◼︎

I owe a great deal of credit to Keith Winston who wrote a helpful article called, "Automating FTP on the Mac" on lowendmac.com. He fully explains the .netrc and shell script bits, and I highly recommend reading the article.