/** * create directory if not exists. */ public function createDirectoryIfNotExists() { if (!file_exists($this->directory) || file_exists($this->directory) && !is_dir($this->directory)) { // folder is not exists, or exists but is not folder (it is a file) $target_directory = str_replace('\\', '/', $this->directory); $target_directory = rtrim($target_directory, '/'); $directory_exp = explode('/', $target_directory); $directory_before_target = ''; foreach ($directory_exp as $a_dir) { if (end($directory_exp) != $a_dir) { $directory_before_target .= $a_dir . '/'; } } \File::create_dir($directory_before_target, end($directory_exp), 0777); unset($directory_before_target, $directory_exp, $target_directory); } }
/** * 内部调用创建目录 * */ public function action_create_dir() { # 目录 $dir = $this->arguments[0]; if (!isset(File::$dir[$dir])) { # 目录不允许操作 $this->show_error('目录不允许操作'); } # 目录 $the_dir = File::$dir[$dir] . $this->arguments[1]; if (File::create_dir($the_dir, $this->arguments[2])) { $this->show_success(); } else { # 记录错误日志 Core::log('system.error.dir.create', array('dir' => $the_dir), LOG_ERR); $this->show_error('执行失败'); } }
/** * 内部调用创建目录 * */ public function action_create_dir() { # 目录 $dir = $this->arguments[0]; if (!isset(File::$dir[$dir])) { # 目录不允许操作 $this->show_error('目录不允许操作'); } # 目录 $the_dir = File::$dir[$dir] . $this->arguments[1]; if (File::create_dir($the_dir, $this->arguments[2])) { $this->show_success(); } else { # 记录错误日志 Core::log('create dir(' . $the_dir . ') error.', 'error'); $this->show_error('执行失败'); } }
/** * 内部调用创建目录 * */ public function action_create_dir() { # 目录 $dir = $this->arguments[0]; if (!isset(\File::$dir[$dir])) { # 目录不允许操作 static::show_error('目录不允许操作'); } # 目录 $the_dir = \File::$dir[$dir] . $this->arguments[1]; if (\File::create_dir($the_dir, $this->arguments[2])) { static::show_message('success', null, 1); } else { # 记录错误日志 \Core::log('create dir(' . $the_dir . ') error.', 'error'); static::show_error('执行失败'); } }
/** * 存数据 * * @param string/array $key 支持多存 * @param $data Value 多存时此项可空 * @param $lifetime 有效期,默认3600,即1小时,0表示最大值30天(2592000) * @return boolean */ public function set($key, $value = null, $lifetime = 3600) { if (is_array($key)) { # 支持多存 $i = 0; foreach ($key as $k => $v) { if ($this->set((string) $k, $v, $lifetime)) { $i++; } } return $i == count($key) ? true : false; } $filename = $this->get_filename_by_key($key); if (!is_dir($this->dir)) { # 创建初始文件夹 if (!File::create_dir($this->dir)) { return false; } } $data = $this->format_data($lifetime, $value); return File::create_file($filename, $data); }
/** * 将上传的文件保存 * * if ($array->check()) * { * // Upload is valid, save it * Upload::save($_FILES['file']); * } * * @param array uploaded file data * @param string new filename * @param string new directory * @param integer chmod mask * @return string on success, full path to new file * @return false on failure */ public static function save(array $file, $filename = null, $directory = null, $chmod = 0644) { if (!isset($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) { // Ignore corrupted uploads return false; } if ($filename === null) { // Use the default filename, with a timestamp pre-pended $filename = uniqid() . $file['name']; } if (Upload::$remove_spaces === true) { // Remove spaces from the filename $filename = preg_replace('/\\s+/', '_', $filename); } if ($directory === null) { // Use the pre-configured upload directory $directory = Upload::$default_directory; } $directory = DIR_WWWROOT . $directory; if (!is_dir($directory) || !is_writable(realpath($directory))) { throw new Exception('Directory ' . $directory . ' must be writable'); } // Make the filename into a complete path $filename = realpath($directory) . DIRECTORY_SEPARATOR . $filename; $filename = str_replace('\\', '/', $filename); File::create_dir(substr($filename, 0, strrpos($filename, '/'))); if (move_uploaded_file($file['tmp_name'], $filename)) { if ($chmod !== false) { // Set permissions on filename chmod($filename, $chmod); } // Return new file path return $filename; } return false; }
/** * 复制目录下的所有目录和文件到另外一个目录 * * @param string $fromdir 源文文件目录 * @param string $todir 目标文件目录 * @param boolean $autocoverageold 是否覆盖已有文件,true覆盖,false跳过 * @param string $storage 物理存储组,不传则为默认 * @return array($dook,$doerror) */ public static function copy_dir($fromdir, $todir, $autocoverageold = true, $storage = 'default') { $fromdir = rtrim($fromdir, '\\/') . DS; $todir = rtrim($todir, '\\/') . DS; if ($fromdir == $todir) { return array(0, 0); } $info1 = File::check_and_get_path($fromdir); $info2 = File::check_and_get_path($todir); if (File::can_do_run($storage)) { if (!is_dir($fromdir)) { return array(0, 0); } # 完成数 $donum = array(0, 0); if (!is_dir($todir)) { # 创建目标目录 File::create_dir($todir, false, $storage); } # 列出目录中当前级别的目录和文件 $files = glob($fromdir . '*'); foreach ($files as $file) { # 目标文件 $tofile = $todir . basename($file); if (is_dir($file)) { # 如果当前是目录,则移动目录 # 移动目录 $donum2 = File::copy_dir($file, $tofile, $autocoverageold, $storage); if ($donum2) { $donum[0] += $donum2[0]; $donum[1] += $donum2[1]; } } else { # 文件 if ($autocoverageold && file_exists($tofile)) { //覆盖已有文件 @unlink($tofile); } if (@copy($file, $tofile)) { $donum[0]++; } else { $donum[1]++; } } } return $donum; } else { return File::call_http_host($storage, 'file/copy_dir', $info1[0], $info1[1], $info2[0], $info2[1], $autocoverageold); } }
/** * 执行输出JS或CSS * */ protected function output_css_js_file() { $file_paths = $this->get_css_or_js_files_array($this->file, $this->suffix); # 输出目录 $out_dir = DIR_ASSETS . 'p-' . Core::$project . (IS_ADMIN_MODE ? DS . '~admin' : '') . DS; # 输出文件 $out_file = $out_dir . $this->file . ($this->is_min ? '.min' : '') . '.' . $this->suffix; # md5存放的文件 $cache_file = DIR_DATA . 'cache/asset_files_md5_' . Core::$project . (IS_ADMIN_MODE ? '~admin' : '') . '_' . str_replace('/', '~', $this->file) . ($this->is_min ? '.min' : '') . '.' . $this->suffix . '.serialize'; if (is_file($cache_file)) { $asset_files_md5 = (array) unserialize(file_get_contents($cache_file)); } else { $asset_files_md5 = array(); } if (is_file($out_file)) { $changed = false; if ($asset_files_md5) { foreach ($file_paths['file_md5'] as $fullpath => $md5) { $debug_path = Core::debug_path($fullpath); if (!isset($asset_files_md5[$debug_path]) || $asset_files_md5[$debug_path] != $md5) { $changed = true; break; } } } else { if (current($file_paths['file_md5']) != md5_file($out_file)) { $changed = true; } } } else { $changed = true; } if ($changed) { $content = ''; if (isset($file_paths['file']) && $file_paths['file']) { foreach ($file_paths['file'] as $full_path) { $content .= file_get_contents($full_path); } } if (isset($file_paths['main']) && $file_paths['main']) { foreach ($file_paths['main'] as $file => $full_path) { # 内容 if (true !== $full_path) { $content .= file_get_contents($full_path); } # 当前文件的扩展 if (isset($file_paths['extends'][$file]) && $file_paths['extends'][$file]) { $content .= CRLF . file_get_contents($file_paths['extends'][$file]); } # 加入模块 if (isset($file_paths['modules'][$file]) && $file_paths['modules'][$file]) { foreach ($file_paths['modules'][$file] as $file2 => $full_path2) { $content .= CRLF . file_get_contents($full_path2); # 模块文件的扩展文件 if (isset($file_paths['extends'][$file2]) && $file_paths['extends'][$file2]) { $content .= CRLF . file_get_contents($file_paths['extends'][$file2]); } } } } } if ($this->suffix === 'css') { $this->add_css_image_version($content); if (isset($file_paths['prease_css']) && $file_paths['prease_css']) { # 处理LESS,和SCSS $this->prease_css($out_file, $file_paths['prease_css'], $content); } } # 创建文件夹 File::create_dir(dirname($out_file)); # 保存文件 if (File::create_file($out_file, $content)) { # 写入文件 foreach ($file_paths['file_md5'] as $full_path => $md5) { $debug_path = Core::debug_path($full_path); $asset_files_md5[$debug_path] = $md5; } # 排序2次的用途是保证一些特殊情况下排序能得到纠正,比如有2个key分别是 test1.js 和 test.js 在排序时会有bug asort($asset_files_md5); asort($asset_files_md5); $old_md5_content = serialize($asset_files_md5); if (!is_file($cache_file) || md5($old_md5_content) != md5_file($cache_file)) { # 保存MD5列表 File::create_file($cache_file, $old_md5_content); } $this->output_by_file($out_file); } else { $this->output_by_content($content); } } else { $this->output_by_file($out_file); } }
public function create_dir($basepath, $name, $chmod = null) { return \File::create_dir($basepath, $name, $chmod, $this); }
public function action_confirm() { if (Input::method() == 'POST') { if (Input::post('id') == "") { $shop = Model_shop::forge(array('name' => Input::post('name'), 'postal_code' => Input::post('postal_code'), 'pref' => Input::post('pref'), 'address' => Input::post('address'), 'detail' => Input::post('detail'), 'category' => Input::post('category'), 'catchphrase' => Input::post('catchphrase'), 'hp_url' => Input::post('hp_url'), 'tel' => Input::post('tel'), 'image_path' => Input::post('image_path'), 'holiday' => Input::post('holiday'), 'open_hh' => Input::post('open_hh'), 'open_mm' => Input::post('open_mm'), 'close_hh' => Input::post('close_hh'), 'close_mm' => Input::post('close_mm'))); } else { $shop = Model_shop::find(Input::post('id')); $shop->set(array('name' => Input::post('name'), 'postal_code' => Input::post('postal_code'), 'pref' => Input::post('pref'), 'address' => Input::post('address'), 'detail' => Input::post('detail'), 'category' => Input::post('category'), 'catchphrase' => Input::post('catchphrase'), 'hp_url' => Input::post('hp_url'), 'tel' => Input::post('tel'), 'image_path' => Input::post('image_path'), 'holiday' => Input::post('holiday'), 'open_hh' => Input::post('open_hh'), 'open_mm' => Input::post('open_mm'), 'close_hh' => Input::post('close_hh'), 'close_mm' => Input::post('close_mm'))); } if ($shop and $shop->save()) { $tmpDir = Utility::convertDirectory(DOCROOT . "files/temp/"); $filesDir = Utility::convertDirectory(DOCROOT . "files/"); $shopsImgDir = Utility::convertDirectory(DOCROOT . "files/" . $shop->id . "/"); try { File::delete($filesDir . $shop->image_path); } catch (Exception $e) { } try { File::create_dir($filesDir, $shop->id, 0755); } catch (Exception $e) { } File::copy($tmpDir . $shop->image_path, $shopsImgDir . $shop->image_path); File::delete($tmpDir . $shop->image_path); Session::set_flash('success', e('Added shop #' . $shop->id . '.')); } else { Session::set_flash('error', e('Could not save shop.')); } $this->template->header = View::forge('docs-header-simple.php'); $this->template->error = View::forge('error.php'); $this->template->content = View::forge('shop/completeRep.php'); $this->template->sns = View::forge('sns.php'); $this->template->footer = View::forge('footer.php'); $this->template->daialog = View::forge('daialog.php'); } else { $this->template->header = View::forge('docs-header-simple.php'); $this->template->error = View::forge('error.php'); $this->template->content = View::forge('shop/confirm.php'); $this->template->sns = ""; $this->template->footer = View::forge('footer.php'); $this->template->daialog = View::forge('daialog.php'); } }
/** * アップ画像:ファイル保存 * * @param string $save_dir * @param string $file_name * @param boolean $mime_check MIME_TYPE(image/*)チェック有無 * @throws \RuntimeException * @throws RuntimeException */ private function save_upload_file($save_dir, $file_name, $mime_check = true) { $upload_key = static::conf_get('upload_key'); $max_size = static::conf_get('max_size'); if (isset($_FILES[$upload_key])) { $error = $_FILES[$upload_key]['error']; // 複数ファイルの同時アップロードチェック if (is_array($error)) { throw new \RuntimeException('Simultaneous upload of multiple files is not allowed.'); } // エラーチェック switch ($error) { case UPLOAD_ERR_INI_SIZE: throw new \RuntimeException('Size over error: max size => ' . $max_size . 'byte'); case UPLOAD_ERR_FORM_SIZE: throw new \RuntimeException('Max size allowed by the form.'); case UPLOAD_ERR_PARTIAL: throw new \RuntimeException('File is corrupted.'); case UPLOAD_ERR_NO_FILE: throw new \RuntimeException('File has not been selected.'); case UPLOAD_ERR_NO_TMP_DIR: throw new \RuntimeException('Tmp directory is not found.'); case UPLOAD_ERR_CANT_WRITE: throw new \RuntimeException('Tmp data creation failed.'); case UPLOAD_ERR_EXTENSION: throw new \RuntimeException('Extension is invalid.'); } // アップ画像:ファイル名 $name = $_FILES[$upload_key]['name']; // アップ画像:一時ファイル名 $tmp_name = $_FILES[$upload_key]['tmp_name']; // アップ画像:ファイルサイズ $size = $_FILES[$upload_key]['size']; // アップ画像:拡張子 self::$_extension = $this->get_ext(); // アップ画像:MIME-TYPE self::$_mime_type = $this->get_mime_type(); // サイズ上限チェック if ($size > $max_size) { throw new \RuntimeException('Size over error: max size => ' . $max_size . 'byte'); } // 不正なファイルでないかチェック if (!is_uploaded_file($tmp_name)) { throw new \RuntimeException('uploaded file is invalid.'); } // MIME-TYPEチェック if ($mime_check) { if (strpos(self::$_mime_type, 'image/') !== 0) { throw new \RuntimeException('uploaded file is invalid format.'); } } // ディレクトリ正当性 $path = realpath($save_dir); if (!$path) { \File::create_dir(dirname($save_dir), static::client_id()); } // ファイル保存 if (!move_uploaded_file($tmp_name, $save_dir . $file_name)) { throw new \RuntimeException('uploaded file save failed.'); } // ファイルパーミッション変更 if (!chmod($save_dir . $file_name, 0666)) { throw new \RuntimeException('uploaded file permission change failed.'); } } else { throw new RuntimeException('uploaded file is not found.'); } }
/** * Extracts a file * * @param string $dest Destination * @param array|null $entries Files to extracted. * * @throws \InvalidPathException when the basepath does not exist or is not writable. * @throws \FileAccessException when the basepath is not writable. * * @return object|bool Current instance. Return false on failure */ public function extractTo($dest, $entries = null) { if (!\File::exists($dest)) { \File::create_dir(pathinfo($dest, PATHINFO_DIRNAME), pathinfo($dest, PATHINFO_BASENAME)); } $result = $this->_zip_archive->extractTo($dest, $entries); if ($result) { return $this; } else { return false; } }