Fork me on Github
Fork me on Github

Joe Dog Software

Proudly serving the Internets since 1999

Process File Uploads With Fido

by Jeff Fulmer | Published: 2025-12-31 12:16:31

Did you ever want to process a file immediately after it was uploaded via FTP? You could have the upload script execute a remote command after the file is uploaded. That requires shell access that you may or may not be able to grant. On the server, you could run a processing script every minute via cron, but that can get messy.

Fido provides an alternative method.

Starting with version 1.0.7, Fido can monitor a file or directory by its modification date. When the date changes, Fido launches a script. We can use this feature to process files uploaded via FTP.

In this example, we’ll monitor a directory. In fido.conf, we'll set up a file block that points to a directory. (For more information about configuring Fido, see the user's manual. This is our configuration:

/home/jdfulmer/incoming {
  rules  = modified
  action = /home/jdfulmer/bin/process
  log    = /home/jdfulmer/var/log/fido.log
}

With this configuration, Fido will continuously monitor/home/jdfulmer/incoming for changes. When a file is uploaded, the date will change, and fido will launch /home/jdfulmer/bin/process. Pretty sweet, huh?

Not quite. The modification date will change when the second ftp lays down the first bite. Our script would begin processing the file before it’s fully uploaded. How do we get around that? We can make our script smarter.

For this exercise, I’m going to move the uploaded files from the incoming directory to my home directory. Here's a script that will do that:

#!/bin/sh
PREFIX="/home/jdfulmer/incoming"
FILES=$(ls $PREFIX)
for F in $FILES ; do
 while [ -n "$(lsof | grep $F)" ] ; do
   sleep 1
 done
 mv $PREFIX/$F /home/jdfulmer
done

In order to ensure the file is fully uploaded, I check lsof for its name. If there’s an open file handle with that name, the script will continue to loop until it’s closed. When the while loop breaks, the script moves the file.

There's just one more thing to think about. When the script moves the file, what happens to the directory Fido is watching? Yes. Its modification date changes. In my example, process runs a second time but does nothing since nothing is there. Depending on your situation, you may need to make the script a little smarter.

Happy Hacking