这是一种竞争条件.你应该这样做:
$filepath = '/media/static/css/common.css';
// is_dir is more appropriate than file_exists here
if (!is_dir(dirname($filepath))) {
if (true !== @mkdir(dirname($filepath), 0777, TRUE)) {
if (is_dir(dirname($filepath))) {
// The directory was created by a concurrent process, so do nothing, keep calm and carry on
} else {
// There is another problem, we manage it (you could manage it with exceptions as well)
$error = error_get_last();
trigger_error($error['message'], E_USER_WARNING);
}
}
}
参考: