我有以下代码:
$data['user_data'] = $this->session->userdata('user');
$user_id = $data['user_data']["employee_id"];
$f_name = $data['user_data']["f_name"];
$s_name = $data['user_data']["s_name"];
$username = $data['user_data']["user_name"];
$title = $data['user_data']["title"];
$is_active = $data['user_data']["is_active"];
$employee_type = $data['user_data']["employee_type"];
$month = $data['user_data']["month"];
$year = $data['user_data']["year"];
$current_month = date('F');
$current_year = date('Y');
$folder_name = $current_month . $current_year;
$directory = "./files/" . $current_month . $current_year;
$directory_name = dirname($directory);
$last_base_name = basename($directory);
$previous_month = $month.$year;
$folder_name = $previous_month;
echo 'folder name : ......before checking ....'. $folder_name.'</br>';
$filename = "./files/" . $previous_month . "/";
echo 'DIRECTORY: .......'.$filename.'</br>';
echo 'File Name' . $previous_month . '</br>';
if (!file_exists($filename)) {
echo 'Directory doesnt exist' . '</br>';
mkdir("./files/".$previous_month."/", 0777);
echo "The directory $previous_month was successfully created.";
$approved_file_name = "./files/" . $previous_month . "/approved";
if (!file_exists($approved_file_name)) {
mkdir("./files/" . $previous_month . "/approved", 0777);
echo 'The directory approved was created';
}
$un_approved_file_name = "./files/" . $previous_month . "/un_approved";
if (!file_exists($un_approved_file_name)) {
mkdir("./files/" . $previous_month . "/un_approved", 0777);
echo 'The directory un_approved was created';
}
我想从上面的代码创建目录,例如March2014 当我运行脚本时,我收到以下错误: 消息:mkdir():无效的参数 请告知什么是不对的? 当我尝试创建以下类型的目录,例如March,它是成功的
答案 0 :(得分:1)
我稍微重构了一下,如果你运行下面的输出你会得到什么?
$data['user_data'] = $this->session->userdata('user');
$user_id = $data['user_data']["employee_id"];
$f_name = $data['user_data']["f_name"];
$s_name = $data['user_data']["s_name"];
$username = $data['user_data']["user_name"];
$title = $data['user_data']["title"];
$is_active = $data['user_data']["is_active"];
$employee_type = $data['user_data']["employee_type"];
//looks for month and year in session, if it doesn't exist set to current
$month = $data['user_data']["month"] ? $data['user_data']["month"] : date('F');
$year = $data['user_data']["year"] ? $data['user_data']["year"] : date('Y');
$folder_name = $month . $year;
//this will add it to files folder under directroy of this file, add /../ etc to navigate to different folder
$folder_path = dirname(__FILE__) . "/files/" . $folder_name;
echo "Createing folder structure under [$folder_path] <br />";
$folder_path_unapproved = $folder_path . "/unapproved";
$folder_path_approved = $folder_path . "/approved";
/**
you could make this following bit a function and call it twice with above strings
**/
if (!file_exists($folder_path_unapproved)) {
echo "Directory doesnt exist $folder_path_unapproved <br/>";
//uses the recursive flag to create the parent directory if it doesn't exist
$created = mkdir($folder_path_unapproved, 0777, true);
if($created) {
echo "The directory $folder_path_unapproved was successfully created.";
} else {
echo "Error createing the directory $folder_path_unapproved.";
}
}
if (!file_exists($folder_path_approved)) {
echo "Directory doesnt exist $folder_path_approved <br/>";
//uses the recursive flag to create the parent directory if it doesn't exist
$created = mkdir($folder_path_approved, 0777, true);
if($created) {
echo "The directory $folder_path_approved was successfully created.";
} else {
echo "Error createing the directory $folder_path_approved.";
}
}