/** * Save * @param string $model * @return boolean or redirect on success, sets message on success */ protected function save($model, $redirect_to = false, $success = "Successfully Saved") { if ($model->is_posted()) { $id = $model->id; if (!$model->author_id && !$_POST[$this->model_name]['author_id']) { $model->author_id = $this->current_user->id; } if ($model->update_attributes($_POST[$this->model_name])) { //clear cache - rely on filename format of $id_ foreach (File::scandir(CACHE_DIR) as $file) { if (($pos = strpos($file, $model->id . '_')) == 0 && $pos !== false) { unlink(CACHE_DIR . $file); } } if ($redirect_to == "edit") { $redirect_to = "edit/" . $id; } elseif ($this->allow_crops) { $redirect_to = "crop/" . $id; } elseif (!$redirect_to) { $redirect_to = "index"; } Session::add_message($this->display_name . " " . $success); $this->redirect_to($redirect_to); } } return false; }
echo 'Directory does not exist.'; exit; } //遍历目录取得文件信息 $file_list = array(); if ($handle = opendir($current_path)) { $i = 0; while (false !== ($filename = readdir($handle))) { if ($filename[0] == '.') { continue; } $file = $current_path . $filename; if (is_dir($file)) { $file_list[$i]['is_dir'] = true; //是否文件夹 $file_list[$i]['has_file'] = count(File::scandir($file)) > 2; //文件夹是否包含文件 $file_list[$i]['filesize'] = 0; //文件大小 $file_list[$i]['is_photo'] = false; //是否图片 $file_list[$i]['filetype'] = ''; //文件类别,用扩展名判断 } else { $file_list[$i]['is_dir'] = false; $file_list[$i]['has_file'] = false; $file_list[$i]['filesize'] = filesize($file); $file_list[$i]['dir_path'] = ''; $file_ext = strtolower(array_pop(explode('.', trim($file)))); $file_list[$i]['is_photo'] = in_array($file_ext, $ext_arr); $file_list[$i]['filetype'] = $file_ext;
public function random_image($directory = "images", $class = "", $alt = "") { $images = File::scandir(PUBLIC_DIR . "images", "[a-z0-9_-]\\.jpg|gif"); $image = $images[rand(0, count($images))]; return "<img src='/" . $directory . "/" . $image . "' alt='" . $alt . "' class='" . $class . "' />"; }
public function tocpjs($path) { if (!is_dir($this->devpath . $path)) { if (strpos(strtolower($path), '.js') == strlen($path) - 3) { if (file_exists($this->propath . $path) && filemtime($this->devpath . $path) < filemtime($this->propath . $path)) { return; } File::creat_dir_with_filepath($this->propath . $path); $buffer = file_get_contents($this->devpath . $path); //如果传入的是JS文件则压缩 if (!$this->needcomp) { file_put_contents($this->propath . $path, $buffer); } else { $packer = new JavaScriptPacker($buffer, 'Normal', true, false); file_put_contents($this->propath . $path, $packer->pack()); } } else { @copy($this->devpath . $path, $this->propath . $path); } return; } //如果传入的参数是目录 $handle = File::scandir($this->devpath . $path); foreach ($handle as $file) { if ($file != '.' && $file != '..') { $dir = $path . '/' . $file; //当前文件$dir为文件目录+文件 $this->tocpjs($dir); } } return; }
/** * 逐一对目录下的所有文件(包括子目录文件)进行处理 * * @param string $path * @param function $callback */ public static function work_with_file($path, $callback) { if (is_callable($callback)) { $fres = File::scandir($path); foreach ($fres as $fn) { if (is_file($path . '/' . $fn)) { $callback($path . '/' . $fn); } else { File::work_with_file($path . '/' . $fn, $callback); } } } }