2018 Drupal tip: How to fix “The file could not be uploaded” file and folder permissions

Abstract image of building floors that have different colors and are numbered

How to fix the Drupal “The file could not be uploaded” error.

Overview

I can’t tell you how often I find myself googling or revisiting a ‘unknown‘ issue just for it to turn out to be a known unknown issue.

In this particular case it has to do with Drupal’s file upload. This ‘issue’ affects Drupal 7.57 and I imagine all versions, mainly because it’s not an issue with Drupal but more to do with configuration and setup.

Here’s how to resolve it:

The Problem

Folder permissions, specially the field and field/images folder which can be found at this path sites/default/files/field/image

The Answer

Change the Read-Write Access

If you can ssh into your server and goto sites/default/files then type the following command into your terminal:

$ chmod 777 field

$ chmod 777 field/image

You can try testing upload again, if this resolves the issue. If not you’ll have to change the ownership of the folders also.

Change the Owner and Group

First type:

$ ls -l

This will print out something that looks like this: drwxrwxrwx 2 "owner name" "group name" 4096 Mar 5 15:06 image

what you need to change is: “owner name” “group name” – minus the quotes obviously.

Now that we know what the owner name and group name for the folder we’re trying to change is, we do that same for another file in the files folder i.e. a folder like css.

Once you’ve done that type the following:

$ chown "new owner name:new group name" field

$ chown "new owner name:new group name" field/image

Again, don’t add the quotes or the any of the spaces.

Now this should resolve the issue, so try uploading again.

How-to Create A Domain Alias In Your Django Development Environment

Close-up of a dark pink/purple tint computer keyboard

Quickly Setup domain routing for multiple Django sites

Overview

Recently, while working on a new Django project I need to create two Django sites that referred to each other. One would be the main domain i.e. mysite.com and the other would be a subdomain i.e. hello.mysite.com.

So I started wondering if there was a way to test this locally in my development environment, and after 2 days of searching online I really found nothing and started to think it was impossible.

However, after speaking with a good friend of mine, Chaz Hill, who happens to be a really kick-ass System Administrator he pointed me in the right direction. I was trying to do all this with Vagrant at the time but in this post I’ll show you how you can do it the normal way, through the shell.

Configure Django

Create your Django project and app, then launch the Django development server using port 80, this is the important part (even though I’ve been using Django for sometime now, it totally slipped my mine I could set the port to 80).

Fix Permissions Error

Now if you tried running this without sudo – you will get an permissions error, the fix of course is to do this => sudo python manage.py runserver 127.0.0.1:80 

Pic of Django development server running in shell and in the foreground the hosts file.

Pic of Django development server running in shell and in the foreground the hosts file.

Run Django Development Server

With the Django development server running, open your hosts file, /etc/hosts and add 120.0.0.1 mysite.com – save the file and goto your browser and type mysite.com, you’ll happily see your Django site running ?

A Quick Tip On Creating An Easy Maintenance Mode Page In Django Using Fabric

Abstract image of wires forming a web

I wanted to share this process that I’ve figured out since I
couldn’t find a solid answer anywhere online, I hope it helps someone
and saves them time and headaches. The problem, “How to put your Django site into maintenance mode” –  the easy way with Fabric.

Truth be told I figured this out by combining info from How to use mod_rewrite rules to easily enable web site “maintenance” modes by Meitar Moscovitz this article pointed me in the right direction.

So here is the solution for using Fabric to put your Django site into maintenance mode;

1. First make sure you create a empty file on your server, this will
be your toggle file (or at least this is what I call it). We’ll use this
to check whether or not the maintenance mode is on or off.

In this case I created a file called “maintenance-mode-on” and put it
in on my server that’s serving the static content for my Django site.

2. In your .htaccess or httpd.conf file add this,

RewriteEngine On
# If this file (toggle file) exsists then put the site into maintenance mode
RewriteCond /path/to/where/your/toggle/file/is/located/on/the/server -f
# If coming from approved ip address, then don't put it into maintenance mode,
# here I'm using HTTP:x-forwarded-for in place of REMOTE_ADDR this is because some users (or your yourself) might
# arrive to the site via a proxy server, so it's more accurate to use HTTP:x-forwarded-for to get the real ip address,
# Also please note that the ip address below has '\' in them because HTTP:x-forwarded-for returns or stores the ip address as a comma delimited list
RewriteCond %{HTTP:x-forwarded-for} !^127\.127\.127\.127$
# redirect to the maintenance mode page
RewriteRule ^(.+) /path/to/the/maintenance/mode/directory/or/index.html/$1 [L]
view raw gistfile1.txt hosted with ❤ by GitHub

3. Once that’s done you can now create two new Fabric commands;

# One for renaming the toggle file from 'maintenance-mode-off' to 'maintenance-mode-on', this will turn on the maintenance mode the next time someone refreshes the page or clicks on a link
def mm_on():
with cd('~/path/to/where/your/toggle/file/is/located/on/the/server'):
run('mv maintenance-mode-off maintenance-mode-on')
# And this command turns the maintenance mode off, again by renaming the "toggle file".
def mm_off():
with cd('~/path/to/where/your/toggle/file/is/located/on/the/server'):
run('mv maintenance-mode-on maintenance-mode-off')
view raw fab.py hosted with ❤ by GitHub

And presto that’s it, you can now put your site into maintenance mode, make the necessary changes and database backups or whatever, without having to worry that users are still in or on the site.