After formally contacting phpcms module development, I developed several functional modules. Among them, I encountered the problem of batch uploading of images. So I started to explore the usage of swfupload in phpcms.
You can directly specify the image group in the content type that comes with phpcms. However, this image group function is not what I want to use. I need to upload an entire static html file. Need
Able to find a way to upload the entire folder. And be able to keep the original file name.
The purpose is summarized as follows:
1, Do not change the file and directory structure of the system.
2, realize the function of uploading multiple attachments.
3, you can get the folder name after uploading.
phpcms has its own attachment upload function. I want to use the swfupload function, and this function is integrated into the attachment upload function of phpcms. Then what I have to do is to extract it and modify it.
The first step is to study how to call this.
First, open firebug in the firefox browser and open the network panel. Find the button called up by swfupload in phpcm. See what connection the system is requesting.
Let’s find this method in this controller in this module.
Inside phpcms/modoules/attachemet/attachemts.php
Open it and take a look, the code is as follows
Then get the parameter args of swfupload from $_GET, and then verify the key. If the key passes, parse the args. Get the website id, get the website settings, and get the size of the attachment allowed to be uploaded. Get it from the cookie List of unused attachments.
Set various displays in the template. The last and most critical thing is that it uses the swfupload template. That means I need to find this template and see how swfupload is introduced.
The template is here: phpcms/modules/attachment/templates/swfupload.tpl.php
Open the template file. A bunch of files are introduced in the template file:
After that is the style file of swfupload and the necessary JS. Here a system function initupload is called. What does this function do?
Don’t underestimate this line. The entire swfupload configuration is here.
Let’s look for this function.
Found traces of it in phpcms/modules/attachment/functions/golable.func.php. The code is as follows. The main function of this function is to configure the swfupload plug-in.
button_image_url: "",
button_width: 75,
button_height: 28,
button_placeholder_id: "buttonPlaceHolder",
button_text_style: "",
button_text_top_padding: 3,
button_text_left_padding: 12,
button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
button_cursor: SWFUpload.CURSOR.HAND,
file_dialog_start_handler: fileDialogStart,
file_queued_handler: fileQueued,
file_queue_error_handler:fileQueueError,
file_dialog_complete_hand ler:fileDialogComplete,
upload_progress_handler: uploadProgress, 🎜> upload_complete_handler:uploadComplete
wfupload.tpl. php
This template uses a js to control
> ul class="attachment-list" id="fsUploadProgress">
In the next step, we need to trigger the swf upload method after clicking on the file
You will find the following code in the code. The swfu.startUpload() method is called here. This method is defined in swfupload.js. We don’t need to pay attention to it.
How to call this thing in my program
First of all, we need to introduce these necessary files into the template that appears in this control
Then write such a tag in our template where you want to place swfupload
This place is the stuff inside the if that I didn’t pay attention to before.
Take it out and take a look
首先它载入了系统的attachment类.并且用到了里面的方法.
程序对上传成功做了echo操作.返回的东西是 返回了编号,上传后的地址,拓展名,文件名.
这些东西是给谁用的啊 我们还得回去看配置文件.
配置文件里面有一段是上传过程中各个事件将触发的方法. 有开始上传的.有上传成功的,有上传失败的.等等.
我们可以看见有一个方法是file_dialog_complete_handler:fileDialogComplete,
其实这些已经升级到swfupload的范畴了.有兴趣可以去研究研究
然后我们在phpcms/static/swfupload/handler.js里面找到这个方法.
看见上传成功后echo出来的数据被解析了.
解析的方法如下
关键的地方来了.我们在swfupload方法里面不是有个attachment的系统类的实例么
真正上传附件是在这里实现的.我们调用了attachment里面的upload方法来实现了文件的上传.
这个attachment文件里面的upload方法在系统类里面 也就是phpcms/libs/classes/attachment.class.php里面
在这个类里面我们可以找到upload方法里面有这样一行
到这里我们就理清思路了.
系统是这么运行的
首先在模板里面引用swfupload(配置文件是用函数生成的)->上传文件->attachment模块里的swfupload方法处理(使用系统的attachment类里面的upload方法循环上传附件.并返回结果给swfupload方法)->处理结果通过swfupload的方法(fileDialogComplete)返回给页面.
在上面我们已经实现了在模板里面引入swfupload.但是我们使用的配置文件和上传附件的方法等都是系统原来自带的.并不能实现我想要的目录结构和文件命名方法.怎么办..
改.
怎么改,首先们要把配置文件改掉. 在自己的模块里面的functions文件夹里面建立自己的函数.我们用自己的函数名称 文件命名为global.func.php这样系统会通过auto_load把我们的函数加载
进去我们把系统中attachment模块functions文件夹下面的global.func.php里面的initupload函数全盘拷贝进来.只修改其中的一行
然后我们去改系统的attachment类 我们在自己的模块下的classes文件夹下面建立一个myattachment.class.php
写一个我们自己的类.去集成系统的attachment类.(记得吧里面的私有方法copy过来.)我们需要修改几行.首先一点是吧upload方法里面的上传目录改掉.然后是改掉文件名的命名方法.
$this->field = $field;
$this->savepath = $this->upload_root.$this->upload_dir.date('Ymd');//这里我们需要修改下.也可以不修改.在我们实例化这个类的时候再来指定目录.
$this->alowexts = $alowexts;
$this->maxsize = $maxsize;
$this->overwrite = $overwrite;
$uploadfiles = array();
$description = isset($GLOBALS[$field.'_description']) ? $GLOBALS[$field.'_description'] : array();
if(is_array($_FILES[$field]['error'])) {
$this->uploads = count($_FILES[$field]['error']);
foreach($_FILES[$field]['error'] as $key => $error) {
if($error === UPLOAD_ERR_NO_FILE) continue;
if($error !== UPLOAD_ERR_OK) {
$this->error = $error;
return false;
}
$uploadfiles[$key] = array('tmp_name' => $_FILES[$field]['tmp_name'][$key], 'name' => $_FILES[$field]['name'][$key], 'type' => $_FILES[$field]['type'][$key], 'size' => $_FILES[$field]['size'][$key], 'error' => $_FILES[$field]['error'][$key], 'description'=>$description[$key],'fn'=>$fn);
}
} else {
$this->uploads = 1;
if(!$description) $description = '';
$uploadfiles[0] = array('tmp_name' => $_FILES[$field]['tmp_name'], 'name' => $_FILES[$field]['name'], 'type' => $_FILES[$field]['type'], 'size' => $_FILES[$field]['size'], 'error' => $_FILES[$field]['error'], 'description'=>$description,'fn'=>$fn);
}
if(!dir_create($this->savepath)) {
$this->error = '8';
return false;
}
if(!is_dir($this->savepath)) {
$this->error = '8';
return false;
}
@chmod($this->savepath, 0777);
if(!is_writeable($this->savepath)) {
$this->error = '9';
return false;
}
if(!$this->is_allow_upload()) {
$this->error = '13';
return false;
}
$aids = array();
foreach($uploadfiles as $k=>$file) {
$fileext = fileext($file['name']);
if($file['error'] != 0) {
$this->error = $file['error'];
return false;
}
if(!preg_match("/^(".$this->alowexts.")$/", $fileext)) {
$this->error = '10';
return false;
}
if($this->maxsize && $file['size'] > $this->maxsize) {
$this->error = '11';
return false;
}
if(!$this->isuploadedfile($file['tmp_name'])) {
$this->error = '12';
return false;
}
//$temp_filename = $this->getname($fileext);//名称在这里.我们需要修改下
$temp_filename = $file['tmp_name'].$fileext; //修改成原来的系统文件名称.
$savefile = $this->savepath.$temp_filename; $savefile = preg_replace("/(php|phtml|php3|php4|jsp|exe|dll|asp|cer|asa|shtml|shtm|aspx|asax|cgi|fcgi|pl)(.|$)/i", "_\1\2", $savefile); $filepath = preg_replace(new_addslashes("|^".$this->upload_root."|"), "", $savefile); if(!$this->overwrite && file_exists($savefile)) continue; $upload_func = $this->upload_func; if(@$upload_func($file['tmp_name'], $savefile)) { $this->uploadeds++; @chmod($savefile, 0644); @unlink($file['tmp_name']); $file['name'] = iconv("utf-8",CHARSET,$file['name']); $uploadedfile = array('filename'=>$file['name'], 'filepath'=>$filepath, 'filesize'=>$file['size'], 'fileext'=>$fileext, 'fn'=>$file['fn']); $thumb_enable = is_array($thumb_setting) && ($thumb_setting[0] > 0 || $thumb_setting[1] > 0 ) ? 1 : 0; $image = new image($thumb_enable,$this->siteid); if($thumb_enable) { $image->thumb($savefile,'',$thumb_setting[0],$thumb_setting[1]); } if($watermark_enable) { $image->watermark($savefile, $savefile); } $aids[] = $this->add($uploadedfile); } } return $aids; }
在我们自己的控制器里面.我们这个时候就需要加载自己写的类了.
At this point, I have accomplished my goal. I have completed the file upload function I want without changing the system file directory.