修改typecho 上传附件的目录,美化附件的URL
•折腾记录
1020
0
typecho 附件上传的链接让我有点儿强迫症
于是决定将它修改掉usr/uploads/2020/04/xxxxx.jpg
不带年月日的干净链接,这样也不会再目录套目录了~
将其规范为usr/uploads/xxxxx.jpg
打开var/widget/upload.php
备份后替换uploadHandle函数
public static function uploadHandle($file)
{
if (empty($file['name'])) {
return false;
}
$result = Typecho_Plugin::factory('Widget_Upload')->trigger($hasUploaded)->uploadHandle($file);
if ($hasUploaded) {
return $result;
}
$ext = self::getSafeName($file['name']);
if (!self::checkFileType($ext) || Typecho_Common::isAppEngine()) {
return false;
}
$path = Typecho_Common::url(defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR,
defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__);
//创建上传目录
if (!is_dir($path)) {
if (!self::makeUploadDir($path)) {
return false;
}
}
//获取文件名
if (isset($file['tmp_name'])) {
$fileName = time(). '.' . $ext;
$path = $path . '/' . $fileName;
//移动上传文件
if (!@move_uploaded_file($file['tmp_name'], $path)) {
return false;
}
} else if (isset($file['bytes'])) {
$fileName = time(). '.' . $ext;
$path = $path . '/' . $fileName;
//直接写入文件
if (!file_put_contents($path, $file['bytes'])) {
return false;
}
} else {
return false;
}
return array(
'name' => $file['name'],
'path' => (defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR)
. '/' . $fileName,
'size' => $file['size'],
'type' => $ext,
'mime' => Typecho_Common::mimeContentType($path)
);
}