I'm updating a function that was posted, as it would fail if there was no directory. It also returns the final value so you can determine if the actual file was written.
public static function file_force_contents($dir, $contents){
$parts = explode('/', $dir);
$file = array_pop($parts);
$dir = '';
foreach($parts as $part) {
if (! is_dir($dir .= "{$part}/")) mkdir($dir);
}
return file_put_contents("{$dir}{$file}", $contents);
}