/** * 图片上传 * @param array $file 图片文件数组 * @param int $maxSize 允许最大尺寸 * @param array $allowExtension 允许上传图片的扩展名数组 * @param int $is_thumbnail 是否生成缩略图 * @param array $thumbnail_size 缩略图的尺寸和后缀(该参数和$is_thumbnail同时出现同时消失) * @return array */ function upload(array $file, $maxSize, array $allowExtension, $is_thumbnail = 0, array $thumbnail_size) { $year = date("Y"); $month = date("m"); $day = date("d"); $targetPath = $_SERVER['DOCUMENT_ROOT'] . "/uploads/{$year}/{$month}/{$day}"; if (!file_exists($targetPath)) { mkdir($targetPath, 0755, true); } if ($file['files']['size'][0] > $maxSize) { return array('status' => false, 'msg' => '图片文件过大,请选择另外的图片'); } else { $fileParts = pathinfo($file['files']['name'][0]); $tempFile = $file['files']['tmp_name'][0]; if (in_array(strtolower($fileParts['extension']), $allowExtension)) { $uploadFileName = generateTargetFileName($fileParts['extension']); $targetFile = rtrim($targetPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $uploadFileName; move_uploaded_file($tempFile, $targetFile); if ($is_thumbnail) { foreach ($thumbnail_size as $v) { thumbnail($targetFile, $v[0], $v[1], $v[2]); } } return array('status' => true, 'src' => 'http://' . $_SERVER['HTTP_HOST'] . "/uploads/{$year}/{$month}/{$day}/" . $uploadFileName, 'filename' => "/uploads/{$year}/{$month}/{$day}/" . $uploadFileName); } else { return array('status' => false, 'msg' => '不支持的图片格式'); } } }
/** * 资讯内容图片上传 */ public function upload_image() { if (!empty($_FILES)) { $year = date("Y"); $month = date("m"); $day = date("d"); $targetPath = $_SERVER['DOCUMENT_ROOT'] . "/uploads/{$year}/{$month}/{$day}"; if (!file_exists($targetPath)) { mkdir($targetPath, 0755, true); } if ($_FILES['upload']['size'] > C('MAX_SIZE')) { echo "<script type='text/javascript'>\n window.parent.CKEDITOR.tools.callFunction(0, '', '图片不能大于2M');\n </script>"; } else { $fileParts = pathinfo($_FILES['upload']['name']); $tempFile = $_FILES['upload']['tmp_name']; if (in_array(strtolower($fileParts['extension']), C('ALLOW_EXTENSIONS'))) { $uploadFileName = generateTargetFileName($fileParts['extension']); $targetFile = rtrim($targetPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $uploadFileName; move_uploaded_file($tempFile, $targetFile); $fileName = "/uploads/{$year}/{$month}/{$day}/" . $uploadFileName; $funcNum = $_GET['CKEditorFuncNum']; echo "<script type='text/javascript'>\n window.parent.CKEDITOR.tools.callFunction({$funcNum}, '{$fileName}');\n </script>"; } else { echo "<script type='text/javascript'>\n window.parent.CKEDITOR.tools.callFunction(0, '', '不支持的图片格式');\n </script>"; } } } else { $this->redirect('/'); } }