thinkphp3.2 implement the Controller method of uploading pictures,
This paper describes the controller method for uploading images thinkphp3.2. Share to everyone for your reference, as follows:
Public Function file () {$baseUrl = str_replace (' \ \ ', '/', dirname ($_server[' script_name ')); Import (' ORG.Net.UploadFile '); Import (' ORG. Util.services_json '); $upload = new UploadFile (); $upload->maxsize = 3145728; $upload->allowexts = array (' jpg ', ' gif ', ' PNG ', ' jpeg '); $upload->savepath = './uploads/images/'; $info = $upload->uploadone ($_files[' imgfile '); $file _url = $baseUrl. ' Uploads/images/'. $info [' 0 '] [' savename ']; if ($info) {header (' content-type:text/html; Charset=utf-8 '); $json = new Services_json (); echo $json->encode (Array (' ERROR ' = 0, ' url ' = = $file _url)); Exit } else {$this->error ($upload->geterrormsg ()); }}public function File_manager () {import (' ORG. Util.services_json '); $php _path = dirname (__file__). '/'; $php _url = dirname ($_server[' php_self '). '/'; $root _path = $php _path. './uploads/images/'; $root _url = $php _url. './uploads/images/'; $ext _arr = array (' gif ', ' jpg ', ' jpeg ', ' PNG ', ' bmp '); $dir _name = empTyempty ($_get[' dir ')? ': Trim ($_get[' dir '); if (!in_array ($dir _name, Array (', ' image ', ' flash ', ' media ', ' file ')) {echo "Invalid Directory name."; Exit } if ($dir _name!== ") {$root _path. = $dir _name. "/"; $root _url. = $dir _name. "/"; if (!file_exists ($root _path)) {mkdir ($root _path); }//sets the path and URL if (emptyempty ($_get[' path ')) {$current _path = Realpath ($root _path) According to the path parameter. '/'; $current _url = $root _url; $current _dir_path = "; $moveup _dir_path = "; } else {$current _path = Realpath ($root _path). '/' . $_get[' path '; $current _url = $root _url. $_get[' path '; $current _dir_path = $_get[' path '); $moveup _dir_path = Preg_replace ('/(. *?) [^\/]+\/$/', ' $ ', $current _dir_path); } Echo Realpath ($root _path);//Sort form, name or size or type $order = Emptyempty ($_get[' order ')? ' Name ': Strtolower ($_get[' order ');//not allowed. Move to the previous-level directory if (Preg_match ('/\.\./', $current _path)) {echo ' Access is not allowed. '; Exit }//the last character is not/if (!preg_match('/\/$/', $current _path)) {echo ' Parameter is not valid. '; Exit The}//directory does not exist or is not a directory if (!file_exists ($current _path) | |!is_dir ($current _path)) {echo ' directory does not exist. '; Exit }//traversal directory to obtain file information $file _list = array (); if ($handle = Opendir ($current _path)) {$i = 0; while (false!== ($filename = Readdir ($handle))) {if ($filename {0} = = '. ') continue; $file = $current _path. $filename; if (Is_dir ($file)) {$file _list[$i] [' is_dir '] = true;//whether the folder $file _list[$i] [' has_file '] = (count (Scandir ($file)) > 2); Whether the folder contains files $file _list[$i [' filesize '] = 0; File size $file _list[$i [' is_photo '] = false; Whether the picture $file _list[$i [' filetype '] = '; File category, with the extension to determine} else {$file _list[$i] [' is_dir '] = false; $file _list[$i [' has_file '] = false; $file _list[$i] [' filesize '] = filesize ($file); $file _list[$i] [' dir_path '] = '; $file _ext = Strtolower (PathInfo ($file, pathinfo_extension)); $file _list[$i] [' is_photo '] = In_array ($file _ext, $ext _arr); $file _list[$i] [' filetype '] = $file _ext; } $file _list[$i [' filename '] = $filename; The file name, which contains the extension $file _list[$i [' datetime '] = Date (' y-m-d h:i:s ', Filemtime ($file)); File last Modified time $i + +; } closedir ($handle); }//sort Usort ($file _list, ' Cmp_func '); $result = Array (),//relative to the directory above the root directory $result [' moveup_dir_path '] = $moveup _dir_path;//relative to the current directory of the root directory $result [' Current_dir_ Path '] = $current _dir_path;//The URL of the current directory $result [' current_url '] = $current the number of _url;//files $result [' total_count '] = count ($file _list)///File list array $result [' file_list '] = $file _list;//Output JSON string header (' Content-type:application/json; Charset=utf-8 ') ); $json = new Services_json (); echo $json->encode ($result);}
More interested in thinkphp related content readers can view this site topic: "thinkphp Introductory Tutorial", "thinkphp Common Methods Summary", "Smarty Template Primer Basic Tutorial" and "PHP template technology Summary."
It is hoped that this article is helpful to the PHP program design based on thinkphp framework.
http://www.bkjia.com/PHPjc/1123821.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123821.html techarticle thinkphp3.2 Implement the Controller method of uploading pictures, this paper describes the controller method for uploading pictures thinkphp3.2. Share to everyone for your reference, as follows: Public function ...