An improved and enraptured code from TrentTompkins at gmail dot com
Note: Added error response
Note: Added directory detection
Note: Added root detection
Note: Added permissions when creating folder
function file_force_contents($dir, $contents, $flags = 0){
if (strpos($dir, "../") === 0){
$dir = str_replace("..", substr(__DIR__, 0, strrpos(__DIR__, "/")), $dir);
}
$parts = explode('/', $dir);
if(is_array($parts)){
$file = array_pop($parts);
$dir = '';
foreach($parts as $part)
if(!is_dir($dir .= "/$part")){
mkdir($dir, 0777, true);
}
if(file_put_contents("$dir/$file", $contents, $flags) === false ){
return false;
}
}else{
if(file_put_contents("$dir", $contents, $flags) === false ){
return false;
}
}
}
-Oliver Leuyim Angel