/** * 复制目录 * @param string $src 源目录路径 * @param string $dest 目标路径 * @return boolean */ public static function copy_dir($src, $dest) { $return = false; if (corefile::is_dir($src) == true) { if (corefile::new_dir($dest) == true) { $dir_list = corefile::list_dir($src, '*'); if ($dir_list) { foreach ($dir_list as $v) { $v_name = basename($v); $v_dest = $dest . DS . $v_name; $return = corefile::copy_dir($v, $v_dest); if ($return == false) { break; } } } else { $return = true; } } } else { $return = corefile::copy_file($src, $dest); } return $return; }
} unset($config_uploadfile_hibit_type); $file_type = substr(strrchr($_FILES[$upload_post_name]['name'], '.'), 1); if (in_array($file_type, $config_uploadfile_hibit_type_arr) == false || $config_uploadfile_hibit_type_arr == null) { $post_file_sha1 = sha1_file($_FILES[$upload_post_name]['tmp_name']); $upload_view = $oapost->view_list(null, null, null, 'public', $post_type, 1, 1, 0, false, 0, '', $post_file_sha1); $upload_id = 0; $upload_name = $_FILES[$upload_post_name]['name']; if ($upload_view) { //如果文件已经存在,则直接引用 $upload_id = $upload_view[0]['id']; $upload_name = $upload_view[0]['post_name']; } else { //如果文件不存在,则开始转移文件 $file_dest_dir = UPLOADFILE_DIR . DS . date('Ym') . DS . date('d'); if (corefile::new_dir($file_dest_dir) == true) { $file_dest = ''; $file_dest_ls = $file_dest_dir . DS . $_FILES[$upload_post_name]["name"]; if (corefile::is_file($file_dest_ls)) { $file_dest = $file_dest_dir . DS . rand(1, 9999) . $_FILES[$upload_post_name]["name"]; } else { $file_dest = $file_dest_ls; } if (corefile::move_upload($_FILES[$upload_post_name]["tmp_name"], $file_dest) == true) { $post_res = $oapost->add($_FILES[$upload_post_name]['name'], '', $post_type, 0, $post_user, $post_file_sha1, $_FILES[$upload_post_name]['name'], $file_dest, 'public', $_FILES[$upload_post_name]['type']); if ($post_res > 0) { //上传成功,创建记录 $upload_id = $post_res; } else { corefile::delete_file($file_dest); $message = '文件上传失败,无法创建相关数据。';
/** * 生成新的备份文件 * @since 2 * @param coredb $db 数据库操作句柄 * @param string $backup_dir 备份到目录 * @param string $content_dir 文件数据目录 * @return string 备份文件路径 */ function plugbackup(&$db, $backup_dir, $content_dir) { $return = ''; $bool = false; $file_type = 'zip'; $ls_dir = $backup_dir . DS . substr(sha1(rand(1, 99999)), 0, 8); if (!corefile::is_dir($ls_dir)) { $ls_sql_dir = $ls_dir . DS . 'sql'; //创建临时目录 $bool = corefile::new_dir($ls_dir); //创建临时SQL目录 if ($bool == true) { $bool = corefile::new_dir($ls_sql_dir); } //拷贝文件数据 if ($bool == true) { $bool = corefile::copy_dir($content_dir . DS . 'files', $ls_dir . DS . 'content' . DS . 'files'); } if ($bool == true) { $bool = corefile::copy_dir($content_dir . DS . 'logs', $ls_dir . DS . 'content' . DS . 'logs'); } //依次遍历所有数据并拷贝到文件内 foreach ($db->tables as $k => $v) { //创建表目录 $v_table_dir = $ls_sql_dir . DS . $v; if ($bool == true) { $bool = corefile::new_dir($v_table_dir); } else { break; } //计算表内所有字段数据平均长度,得出最终步长 $max = 50; if ($bool == true) { $sql = 'SELECT AVG( LENGTH(`' . implode('`))+AVG(LENGTH(`', $db->fields[$k]) . '`)) as al FROM `' . $v . '`'; $sth = $db->prepare($sql); if ($sth->execute() == true) { $res = (int) $sth->fetchColumn(); if ($res > 500) { $max = 20; } elseif ($res > 1000) { $max = 10; } elseif ($res > 5000) { $max = 1; } } else { $bool = false; break; } } else { break; } //遍历数据写入文件 $p = 0; $p_bool = true; while ($p_bool) { $sql = 'SELECT * FROM `' . $v . '` ORDER BY ' . $db->fields[$k][0] . ' ASC LIMIT ' . $p * $max . ',' . $max; $sth = $db->prepare($sql); if ($sth->execute() == true) { $res = $sth->fetchAll(PDO::FETCH_ASSOC); if ($res) { $file_content = 'INSERT INTO `' . $v . '`(`' . implode('`,`', array_keys($res[0])) . '`) VALUES'; foreach ($res as $v_res) { $file_content .= '('; foreach ($v_res as $v_res_v) { if ($v_res_v == null) { $file_content .= 'NULL,'; } elseif (is_int($v_res_v) == true) { $file_content .= $v_res_v . ','; } else { $file_content .= '\'' . $v_res_v . '\','; } } $file_content = substr($file_content, 0, -1); $file_content .= '),'; } $file_content = substr($file_content, 0, -1) . ';'; $file_table_row = $v_table_dir . DS . $v . '_' . $p . '.sql'; $p_bool = corefile::edit_file($file_table_row, $file_content); $bool = $p_bool; $file_content = null; } else { $p_bool = false; $bool = true; } } else { $p_bool = false; $bool = false; } $p += 1; } } } //将临时文件压缩为压缩包 $backup_file = $backup_dir . DS . time() . '.' . $file_type; if (corefile::is_file($backup_file)) { $bool = corefile::delete_file($backup_file); } if ($bool == true) { $bool = corefile::create_zip($backup_file, $ls_dir); } if ($bool == true) { $return = $backup_file; } else { //失败删除所有临时文件 corefile::delete_file($backup_file); } //删除临时文件 corefile::delete_dir($ls_dir); return $return; }