This article describes the Zend Framework method of implementing the multi-file upload function. Share it with everyone for your reference, the details are as follows:
Step one: Find the library folder in our original tutorial. This is where our Zend class library folder is stored. Now we add the following folders to the library folder: library/Custom/Controller /Plugin/
We have finished adding the folder. Now we have to find the class file for our file upload. Here I named it Upload.php file name. This file is the class file we will use for file upload. Of course, I rewrote this file into a plug-in form in Zend Framework... Part of the file description of Upload.php is as follows (I only give part of the program here, which is a simple multi-file upload class):
<?php class Custom_Controller_Plugin_Upload extends Zend_Controller_Plugin_Abstract { private $uploaddir; //文件上传存路径 private $max_files; //一次性最多上传多少文件 private $max_size; //文件最大量 private $permission; //文件夹是否可以有权限 private $files; private $allowed = array (); //允许上传文件格式 //不允许上传文件格式 private $notallowed = array ("exe", "mp3" ); private $filesname; //文件表单name //图片文件宽度,超过就生成缩略图 private $imagewidth; //图片文件高度,超过就生成缩略图 private $imageheight; public $filearray = array (); //返回多个文件名 public $lastFileName; //返加一个文件名 public $Error; ?>
Step 2: In our previous tutorial on writing a guestbook...find the controller at that time...IndexController.php, and add an action called upload. This upload action is for uploading files. The detailed procedure for using it is as follows:
public function uploadAction() { echo $this->view->render('header.phtml');//显示模版头文件 if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ Zend_Loader::loadClass('Custom_Controller_Plugin_Upload'); $uploadfile=new Custom_Controller_Plugin_Upload( ROOT_DIR.'/public/upload/','uploadfile','10','2048000', array("gif","png","jpg","bmp","jpeg"),600,600); if(''==$uploadfile->Error){ echo '<div class="commentInner" style="width:860px;text-align:center;"> 恭喜您,照片上传成功!请您<a style="color:red;" href= "'.$this->view->baseUrl.'/index/index/">点这里返回</a></div>'; }else{ echo '<div class="commentInner" style="width:860px;text-align:center;"> 对不起,您照片上传有以下错误:'.$uploadfile->error.'请返回重新上传!请您 <a style="color:red;" href="'.$this->view->baseUrl.'/index/upload/"> 点这里返回</a></div>'; } } echo $this->view->render('message/upload.phtml');//显示模版 echo $this->view->render('footer.phtml');//显示模版脚文件 }
Step 3: Add the upload function Form form to our View template folder...: Here I have added an upload.phtml template file in Riga... In order to let everyone better see how to implement uploading. .I will only write the simplest file upload form. It will not be too difficult to implement... We only need to point its POST action to our upload action in this View template file to upload files. Got it..
Finally, all you need is to enter your website address. For example, mine: http://127.0.0.1/zendframework/index/upload/ This only requires you to select the image file in the upload form.. and you can upload it. Okay.. If there is nothing wrong, the file you just uploaded will be under the folder zendframework/public/upload/ in the website directory. There will be no mistakes in the test. Because I only control uploading as image files here. You can change it. into other files you want to upload..
Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.