How To Roll Your Own File-Sharing Service


Whether you were sad to see MegaUpload bite the dust, aren’t keen on the alternatives, or just want to use your own domain name and hosting service to share your files, you’re in luck! Here’s a simple way to set up your own killer drag-and-drop file sharing service with existing software and some code of our own.

Here’s how this neat little system will work:

You’ll have a file you want to share, so you’ll drag it onto an icon on your desktop (Windows) or dock (Mac OS X) and the file will magically upload. The uploader will automatically store a link in your clipboard that you can paste into an email, a chat window, or wherever you want so that you or anyone else can quickly download that file. Additionally, you’ll be able to visit your shared files directory to grab any link again in the future, should you need it, and perform cool tasks like PDF merging and file zipping. If this sounds like something you want to do, you’re in the right place.

What You’ll Need

Here’s what you’ll need to get started:


  1. A domain name and web host with PHP 5 installed (most hosts have PHP5 installed by default). For automated setup, you’ll also want your web host to allow you to schedule cron jobs. (We’ll explain what these do later.) Whichever host you choose, make sure you have the domain name you want to use set up for hosting with FTP (or SFTP) enabled. Keep your username, password and FTP server handy because you’ll need that information for later.
  2. An FTP client for Windows or Mac, plus a basic understanding of how FTP works.
  3. Dropf (if you’re on Windows) or Dockdrop (if you’re on Mac OS X). Note: Dockdrop says it only works with Mac OS X 10.4 and 10.5, but in our tests it worked great with both 10.6 and 10.7 as well.
  4. These PHP scripts we made to give your file-sharing service some extra features.

Once you’ve got all of that together you’re ready to go!

Step One: Configure The Uploader

When you were getting everything together, you either downloaded Dropf for Windows or Dockdrop for Mac. To get either of these tools uploading to your server, you’ll need to provide them with your server’s address and your FTP username and password. This process differs a little based on which tool you’re using, so just read the setup process that’s relevant to you.

Dropf (Windows)


Dropf is very simple to set up, but you’ll need to make sure you have the latest .NET framework installed first. If you don’t, you can download it here. With that out of the way, you just need to follow these steps:

  1. Open up the Dropf application. You’ll see a black file box appear on your desktop. Right click on it and choose FTP Accounts -> Manage.
  2. Click the Add button to create a new FTP account, then enter your server name in the Host field (generally yourdomain.com or ftp.yourdomain.com), your username in the User Name field, and password in the Password field. In most cases you can leave port 21 alone, as it is the default port for FTP. Unless your host uses a different port, you don’t have to change it.

  3. You’ll also need to specify the root folder and the upload folder. With many web hosts, your account starts in the root folder. On some it does not and so you will need to enter a path here. In most cases that path will simply be /htdocs or /yourdomain.com, but you’ll need to check with your web host for the exact path. If you’re uploading to the root folder, you do not need to specify anything in the Upload Folder field. If you’d prefer to upload to a specific folder at your domain name (e.g. putting files in http://yourdomain.com/files/ instead of just http://yourdomain.com/) you can specify the name of that directory here. Of course, you’ll need to create that directory using your FTP application first. We’ll discuss how you can do this later, so don’t worry if you don’t know how.
  4. Finally, enter the URL prefix that will be used to access your uploaded files. Regardless of whether or not you’re using an upload folder or uploading to the root directory of your domain, that URL will look like this: http://yourdomain.com/. Dropf will automatically add any folder you’re using for you.
  5. Click the Check button to check your FTP settings, and if everything works then click the OK button.

That’s it, you’re all set! Now when you drag a file onto Dropf’s desktop icon, it’ll upload a file to your server.

Dockdrop (Mac)

Dockdrop is very easy to set up. Just follow these simple steps:

  1. Open up the Dockdrop application and choose Preferences from the Dockdrop menu (or just press Command+, on your keyboard).
  2. Dockdrop is capable of using FTP, SCP (aka SFTP), and WebDAV to upload. You can use whichever method you prefer, but we’re going to look at setting up FTP because it’s the simplest option. When your preferences first open, the FTP tab should be automatically selected. Check the box next to “Enable FTP upload method” to get started.

  3. Fill in the Server field with the name of your FTP server (which is likely ftp.yourdomain.com or just yourdomain.com, but check with your host to be sure), the Username field with your username, and the Password field with your password.
  4. In the Path field, specify where you want to upload the files on your server. If you want to upload to the root directory of your domain, you may only have to specify as much as a /. You will need to check with your host to find out what the root path is, as it will vary from host to host. If you want to upload to a folder on your domain (e.g. http://yourdomain.com/files), you’ll need to add that to the path as well (e.g. /yourdomain.com/htdocs/files/).
  5. Finally, enter the URL prefix that will be used to access your uploaded files in the Web URL field. It will look something like http://yourdomain.com/ or http://yourdomain.com/files/, depending on whether you used a folder or not.
  6. Click the Test Connection button to make sure everything is working. If it is, click the Done button.

That’s it, you’re all set! Now when you drag a file onto Dockdrop’s icon in the dock, it’ll upload a file to your server.

Step Two: Upload the File-Sharing Scripts


Using your FTP application of choice, you now need to install the PHP script we made to help make your file sharing service a bit better. This is very easy to do. Just open up your FTP application of choice, enter the server information and credentials you used in the previous section to start a connection, and upload the three of the four included scripts (index.php, dirlist.php and CreateZipFile.php — we’ll be dealing with scandir.php later) to the directory where your shared files will live. That’s it!

Note: Not all PHP installations were created equal. These scripts were tested with PHP 5 on Dreamhost, so we know you’re good to go with them. Because PHP may be a little different on various hosts, some parts of the scripts may have trouble running. If you find that certain functions don’t work, verify with your host that you’re allowed to use the fwrite(), scandir(), file_get_contents() and unlink() functions. To use the PDF features, the exec() function also needs to be enabled and GhostScript needs to be installed.

So what will this script do for you? Here’s an overview of the functionality:

  • The scripts will display all the files and folders in your directory in a nice list. Folders will show up first, and files second.
  • You’ll be able to easily click on any file name to view or download the file, or right-click to copy a link to the file.
  • You can tick the boxes next to one or many files in your file list to perform actions in a menu at the bottom of the page. You’ll be able to download a zip of the selected files, merge multiple PDF documents into a single PDF file, get a short URL for selected files (so you can share multiple files easily), and delete any files you don’t want anymore.
  • The script will hide certain files in the directory automatically, such as the script itself and other files you won’t want cluttering up the page.
  • If you add a text file called private.txt to the directory containing the scripts, the directory will not list files and instead show a privacy page.

Note: Mac OS X’s built-in unzipping utility has a little trouble with the ZIP files created by this script. Third-party unarchiving applications, such as The Unarchiver, will work much better.

Bonus Features


There are also a couple of customisation features for those of you who are a little familiar with PHP. You can added the following variables to the top of your index.php script to quickly change and add things:

  • You can specify a custom image file to display at the top of every page by setting the $custom_img_tag variable to an image tag like this:

    <?php $custom_img_tag = ‘<img src=”my_image.jpg” alt=”Alternate Image” width=”128″ height=”128″ />’; ?>

  • You can add a custom message to the bottom of a page by setting the $dir_info variable to whatever message you want to display like this:

    <?php $dir_info=’This is where I keep my most special files.’; ?>

  • You can password-protect any directory by setting the $secret_password variable like this:

    <?php $secret_password = ‘lifehacker’ ?>

    This will hide the contents of a directory. You can reveal it by adding ?pass=yourpassword to the URL. This is, of course, not a very secure method of protecting your files but it will keep them out of search listings and away from the prying eyes of the average viewer — so long as you don’t share the password.

If you have a little PHP know-how, you can, of course, alter this script to add new features or change how it works. You have the source code, so have fun with it if you want to make it do more for you.

CreateZipFile.php, the script that allowed us to easily zip up files in your file-sharing directories, was created by Rochak Chauhan.

Step Three (Optional): Configure a Cron Job


When you upload the PHP scripts to the directory where you want to share files, you’ll have a nice, attractive listing so long as you don’t create any new directories. If you create a new directory, however, it won’t have a copy of the index.php script and therefore won’t look nice or have all the extra features. This is why you need to set up a cron job to tell the other directories to use the file listing script (dirlist.php) as well. How you set up a cron job with your web host will vary, so check your host’s knowledge base, wiki, or whatever to find out the exact process. We’ll explain it generically so you can apply the following steps to any method of creating a cron job.

  1. Connect to your FTP and upload the scandir.php script to the same directory as dirlist.php. If it is not in the same directory, this will not work.
  2. Now, schedule this cron job:

    wget -O /dev/null http://yourdomain.com/scandir.php

    Obviously you want to replace yourdomain.com with your actual domain and add any directories to that URL if you don’t keep scandir.php at the root (e.g. http://yourdomain.com/files/scandir.php).

  3. Schedule this cron job to run every 5, 10 or 15 minutes depending on the frequency you need. Remember, you can always visit that URL manually if you need to run an update right away.

That’s all there is to it. Assuming your server supports the fwrite() and scandir() PHP functions, this script should run without issue. Nonetheless, be sure to test it out to make sure it works with your host’s PHP installation. If you see an index.php file pop up in an empty subdirectories, you know it’s working.


The Cheapest NBN 50 Plans

Here are the cheapest plans available for Australia’s most popular NBN speed tier.

At Lifehacker, we independently select and write about stuff we love and think you'll like too. We have affiliate and advertising partnerships, which means we may collect a share of sales or other compensation from the links on this page. BTW – prices are accurate and items in stock at the time of posting.

Comments


6 responses to “How To Roll Your Own File-Sharing Service”

Leave a Reply