让Mingle Forum上传的图像放置在单独的目录 Let Mingle Forum upload images to isolated directories
As far as I know, the Mingle Forum is my favorite forum plugin of WP, but its only Cons is that the allowed uploading image size has no restriction.
I was a C programmer, but not touched it for almost 5 years, and yes I never touched PHP so far in my life. so it’s tough for me to add the restriction or resizing feature.
Then I realized that’s also ok if I can let it upload images to an isolated directory, since it could be easy to remove those images by whole directory from posts when the storage space is close to run out. Otherwise, all those images would stay together with Media library of WP posts/pages – quite hard to isolate them and get those stuff cleaned.
So you see, we get that standard WP media lib directory, then check and/or add “mf” sub-directory, and it’s perfectly solved.
Pls check the code enclosed at post bottom, feel free to refer but pls add link to this post; plus, pls let me know if there is any potential bug inside since it’s my 1st time to touch PHP.
PS: 1st time to read WP and PHP standard API manual, it’s “loud and clear”, not hard to learn, and I could even debug this stuff without error report – sure this one is easy and I know that. 🙂
Fisherworks今天一直在纠结使用哪个论坛插件的问题
bbpress在快速研究之下没找到合适的中文语言包
simple forum支持图像附件貌似还要个插件,而且这个论坛看起来比较专业,不是俺想要的
于是锁定了方便好配置的Mingle Forum,虽然默认皮肤集成不是很好,但这插件实在是太方便易用好配置了,而且默认支持图像附件的上传
但问题也跟着来了,它的图像附件是跟WP混在一个目录里上传的,而且不被WP“多媒体”管理
这都不算事儿,最关键的是Mingle Forum不限制图像尺寸(虽然显示时可以自动缩图,不会出错)
这样的话,每个用户都来发个5M的图像,这空间……
后来就翻来覆去找英文文档和论坛,民愤极大,却一直只解决显示错位问题,并不添加图像大小限制的功能
无计可施之后,怎么办?
退而求其次,让Mingle Forum上传的图像放在单独的目录下,不与 /wp-contents/uploads/年/月 里的普通多媒体文件混在一起,这样就不怕大图像了,当空间窘迫时,找到Mingle Forum的图像目录,轻松删除即可
好!就这么干!
Fisherworks这辈子也没接触过PHP,学校学的C语言老底都还给老师了,咋办呢?
硬着头皮仔细看了看插件的代码(天可怜见,家里电脑连个UE都没装,愣是在WP的“插件”–>“编辑”里读的代码)
然后就看到上传的这段code,是如何调用标准wp_upload_dir()找到目录和URL的
那么,接下来就是“灵机一动”时间,查一下PHP函数规范,用is_dir查询并增加一个子目录就好了
基本逻辑就是,找到当前多媒体目录,比如wp-contents/uploads/2013/07,查询其下是否有mf目录,有则返回目录和URL,无则新建并返回
然后……就没有然后了,其后Mingle Forum的图像都上传到这个子目录下,成功搞定!
(本文及代码可随意转载,但请标明出处及本文链接,谢谢;另外,如果您认为本代码有任何潜在问题,也请告诉我,毕竟我是第一次碰PHP)
修改之前(点击看大图):
Before the modification (click pic for full resolution viewing):
修改之后:
After the modification:
编辑 mingle-forum/wpf-insert.php
//Fisherworks 20130705
function mf_check_image_dir()
{
$upload_dir = wp_upload_dir();
$path = $upload_dir['path']."/";
$image_dir = $path."mf"."/";
if(is_dir($image_dir))
return $image_dir;
else
{
if (!mkdir($image_dir))
{
return $path;
die('Failed to create folders...');
}
return $image_dir;
}
}
function mf_check_image_url()
{
$upload_dir = wp_upload_dir();
$path = $upload_dir['path']."/";
$url = $upload_dir['url']."/";
$image_dir = $path."mf"."/";
if (is_dir($image_dir))
return $url."mf"."/";
else
{
return $url;
die('Failed to locate mf_image special folder.');
}
}
function MFAttachImage($temp, $name)
{
//GET USERS UPLOAD PATH
$upload_dir = wp_upload_dir();
$path = mf_check_image_dir();
$url = mf_check_image_url();
$u = mf_u_key();
$name = sanitize_file_name($name);
if(!empty($name))
move_uploaded_file($temp, $path.$u.$name);
return "\n[img]".$url.$u.$name."[/img]";
}
// function MFAttachImage($temp, $name)
// {
//GET USERS UPLOAD PATH
// $upload_dir = wp_upload_dir();
// $path = $upload_dir['path']."/";
// $url = $upload_dir['url']."/";
// $u = mf_u_key();
// $name = sanitize_file_name($name);
// if(!empty($name))
// move_uploaded_file($temp, $path.$u.$name);
// return "\n[img]".$url.$u.$name."[/img]";
// }
//Fisherworks 20130705
原创文章,转载请注明: 转载自渔人小径
本文链接地址: 让Mingle Forum上传的图像放置在单独的目录 Let Mingle Forum upload images to isolated directories
打赏 PayPal
文章的脚注信息由WordPress的wp-posturl插件自动生成