Category: DigitalOcean

DigitalOcean Spaces – Learn how to use Spaces with PHP

DigitalOcean Spaces – Learn how to use Spaces with PHP

Using DigitalOcean Spaces with your next PHP project is a beautiful idea. Spaces will give you all the hard drive space that you could ever need. This post can help you build your next PHP project with DigitalOcean Spaces. Building out an infrastructure can be a difficult job for anyone, but DigitalOcean’s services make it a bit easier.

Mounting Object Storage

You could upload directly to S3 compatible storage using a PHP package, but it is easier to mount a bucket and let the file system handle the rest. Read my post on Mounting Object Storage if you need help getting started.

By mounting your bucket to your local file system, it will allow you to have nearly unlimited storage space for your app. In addition to storing uploaded files to object storage, you could also save backups, log files, and anything else you could imagine.

PHP Uploading to DigitalOcean Spaces

Your code will probably be a lot more complicated than the example below. However, I think the code below will suffice for this blog post.

$message = '';
if(!empty($_FILES['uploaded_file'])) {
  $path = 'uploads/';
  $path = $path . uniqid('u', true) . '-' . basename($_FILES['uploaded_file']['name']);

  if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
    $message = 'The file ' . basename( $_FILES['uploaded_file']['name']) . ' has been uploaded';
  } else {
    $message = 'There was an error uploading the file, please try again!';
    $message .= $_FILES['uploaded_file']['error'];
<!DOCTYPE html>
  <title>Upload your files</title>
  <?php echo $message; ?>
  <form enctype="multipart/form-data" action="upload.php" method="POST">
    <p>Upload your file</p>
    <input type="file" name="uploaded_file"></input><br />
    <input type="submit" value="Upload"></input>

The code in the example above shows you how to upload a file to a folder. You will need to change the path to where you mounted your bucket. Of course, you will need to make sure you validate the file type and restrict it to users who are logged in.


Digitalocean Spaces is the best object storage service currently available. While they might not have as much space available as AWS, it is compatible with S3, easy to use, and has a better UI.

Please don’t store any sensitive or confidential information in your buckets. If you must, then please make sure you have the proper settings and those files aren’t publicly exposed.

Image Credit DigitalOcean.

Mounting DigitalOcean Spaces

Mounting DigitalOcean Spaces

I assume you have already created a Space and Droplet on DigitalOcean. If you haven’t, do that now, then come back to this post. In this example I’m using Ubuntu 16.04. I’ve also pointed a domain to this Droplet and put it behind Cloudflare, so Spaces can act as my file repository for my assets, and Cloudflare will deliver them.

Mounting Spaces on your Droplet

  1. SSH into your Droplet and run the following commands:
    • sudo apt-get install automake autotools-dev fuse g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config
    • git clone
    • cd s3fs-fuse
    • ./
    • ./configure
    • make
    • sudo make install
  2. Next you need to create a file with your Spaces API Key and Secret Key
    • echo 'KEY:SECRET_KEY' > ~/.passwd-s3fs
    • chmod 600 ~/.passwd-s3fs
  3. Create a mountpoint for Spaces by running mkdir /spaces
  4. Then you need to append the following to your /etc/fstab file
    s3fs#BUCKET_NAME /spaces fuse allow_other,_netdev,nosuid,nodev,url= 0 0
    Be sure to replace BUCKET_NAME with your actual Spaces name
  5. After saving that file, run sudo mount -a .

Now you have unlimited storage capabilities on that Droplet. You could also install nginx and point the root directory to your Spaces mount point. Doing that would allow you to host as many assets as you want, without worrying about space. Maybe you can create the next Imgur.