Well this is something that I'd quite like to do, but I can't stand php (sorry) and I wanted to have a go at fixing some of the shortcomings, so I've written my own version in python.
This should handle spaces in directory names (as well as other strange characters, but nautilus' quoting rules seem rather obscure, so maybe there are things I've missed here).
You can specify your own shell-type pattern (--names) for matching the image filename (the default is "[Ff]older.jpg").
There is also a switch (-R) to make it work recursively across nested directories.
Download:
custom_icon.py
Code:
$ python custom_icon.py --help
usage: custom_icon.py [options] folder [folder2 ...]
options:
--version show program's version number and exit
-h, --help show this help message and exit
--names=NAMES specification to match image filenames [default:
"[Ff]older.jpg"]
-R, --recursive set icons for folders recursively
$ python custom_icon.py ~/music
/home/user/music ...
done.
$ nautilus -q