Thinkphp3.2 implements the Controller Method for uploading images,

Source: Internet
Author: User

Thinkphp3.2 implements the Controller Method for uploading images,

This article describes how to use thinkphp3.2 to upload images. We will share this with you for your reference. The details are 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 = $ ph P_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) ;}}// set the path and URL if (emptyempty ($ _ GET ['path']) based on the path parameter) {$ current_path = realpath ($ root_path ). '/'; $ 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 ('/( .*?) [^ \/] + \/$/',' $ 1', $ current_dir_path);} echo realpath ($ root_path); // sorting form, name or size or type $ order = emptyempty ($ _ GET ['order'])? 'Name': strtolower ($ _ GET ['order']); // not allowed .. move to the upper-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 the directory if (! File_exists ($ current_path) |! Is_dir ($ current_path) {echo 'Directory does not exist. '; exit;} // retrieve the file information through the directory $ 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 the file $ file_list [$ I] ['filesize'] = 0; // the file size $ file_list [$ I] ['is _ photo '] = false; // whether the image is $ file_list [$ I] ['filetype'] = ''; // file category, use 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; // file name, include the extension $ file_list [$ I] ['datetime'] = date ('Y-m-d H: I: s', filemtime ($ file )); // file last modification time $ I ++;} closedir ($ handle);} // sort usort ($ file_list, 'cmp _ func '); $ result = array (); // The upper-level directory relative to 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_url; // number of files $ result ['total _ count'] = count ($ file_list); // array of file lists $ result ['file _ list'] = $ file_list; // output JSON string header ('content-type: application/json; charset = UTF-8 '); $ json = new Services_JSON (); echo $ json-> encode ($ result );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.