当前位置:首页 > CMS教程 > Thinkphp > 列表

thinkphp3.2实现上传图片的控制器方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-30 11:32:59 浏览: 评论:0 

这篇文章主要介绍了thinkphp3.2实现上传图片的控制器方法,结合实例形式分析了thinkPHP图片文件上传相关的文件类型判断,文件路径及相关属性操作技巧,需要的朋友可以参考下。

本文讲述了thinkphp3.2实现上传图片的控制器方法,分享给大家供大家参考,具体如下:

  1. public function file() 
  2. { 
  3.   $baseUrl = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])); 
  4.   import('ORG.Net.UploadFile'); 
  5.   import('ORG.Util.Services_JSON'); 
  6.   $upload = new UploadFile(); 
  7.   $upload->maxSize = 3145728; 
  8.   $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); 
  9.   $upload->savePath = './uploads/Images/'; 
  10.   $info = $upload->uploadOne($_FILES['imgFile']); 
  11.   $file_url = $baseUrl . 'uploads/Images/' . $info['0']['savename']; 
  12.   if ($info) { 
  13.    header('Content-type: text/html; charset=UTF-8'); 
  14.    $json = new Services_JSON(); 
  15.    echo $json->encode(array('error' => 0, 'url' => $file_url)); 
  16.    exit; 
  17.   } else { 
  18.    $this->error($upload->getErrorMsg()); 
  19.   } 
  20. } 
  21. public function file_manager() 
  22. { 
  23.   import('ORG.Util.Services_JSON'); 
  24.   $php_path = dirname(__FILE__) . '/'; 
  25.   $php_url = dirname($_SERVER['PHP_SELF']) . '/'; 
  26.   $root_path = $php_path . './uploads/Images/'; 
  27.   $root_url = $php_url . './uploads/Images/'; 
  28.   $ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp'); 
  29.   $dir_name = emptyempty($_GET['dir']) ? '' : trim($_GET['dir']); 
  30.   if (!in_array($dir_name, array('', 'image', 'flash', 'media', 'file'))) { 
  31.    echo "Invalid Directory name."; 
  32.    exit; 
  33.   } 
  34.   if ($dir_name !== '') { 
  35.    $root_path .= $dir_name . "/"; 
  36.    $root_url .= $dir_name . "/"; 
  37.    if (!file_exists($root_path)) { 
  38.     mkdir($root_path); 
  39.    } 
  40.   } 
  41. //根据path参数,设置各路径和URL 
  42.   if (emptyempty($_GET['path'])) { 
  43.    $current_path = realpath($root_path) . '/'; 
  44.    $current_url = $root_url; 
  45.    $current_dir_path = ''; 
  46.    $moveup_dir_path = ''; 
  47.   } else { 
  48.    $current_path = realpath($root_path) . '/' . $_GET['path']; 
  49.    $current_url = $root_url . $_GET['path']; 
  50.    $current_dir_path = $_GET['path']; 
  51.    $moveup_dir_path = preg_replace('/(.*?)[^\/]+\/$/', '$1', $current_dir_path); 
  52.   } 
  53.   echo realpath($root_path); 
  54. //排序形式,name or size or type 
  55.   $order = emptyempty($_GET['order']) ? 'name' : strtolower($_GET['order']); 
  56. //不允许使用..移动到上一级目录 
  57.   if (preg_match('/\.\./', $current_path)) { 
  58.    echo 'Access is not allowed.'; 
  59.    exit; 
  60.   } 
  61. //最后一个字符不是/ 
  62.   if (!preg_match('/\/$/', $current_path)) { 
  63.    echo 'Parameter is not valid.'; 
  64.    exit; 
  65.   } 
  66. //目录不存在或不是目录 
  67.   if (!file_exists($current_path) || !is_dir($current_path)) { 
  68.    echo 'Directory does not exist.'; 
  69.    exit; 
  70.   } 
  71. //遍历目录取得文件信息 
  72.   $file_list = array(); 
  73.   if ($handle = opendir($current_path)) { 
  74.    $i = 0; 
  75.    while (false !== ($filename = readdir($handle))) { 
  76.     if ($filename{0} == '.') continue; 
  77.     $file = $current_path . $filename; 
  78.     if (is_dir($file)) { 
  79.      $file_list[$i]['is_dir'] = true; //是否文件夹 
  80.      $file_list[$i]['has_file'] = (count(scandir($file)) > 2); //文件夹是否包含文件 
  81.      $file_list[$i]['filesize'] = 0; //文件大小 
  82.      $file_list[$i]['is_photo'] = false; //是否图片 
  83.      $file_list[$i]['filetype'] = ''; //文件类别,用扩展名判断 
  84.     } else { 
  85.      $file_list[$i]['is_dir'] = false; 
  86.      $file_list[$i]['has_file'] = false; 
  87.      $file_list[$i]['filesize'] = filesize($file); 
  88.      $file_list[$i]['dir_path'] = ''; 
  89.      $file_ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); 
  90.      $file_list[$i]['is_photo'] = in_array($file_ext, $ext_arr); 
  91.      $file_list[$i]['filetype'] = $file_ext; 
  92.     } 
  93.     $file_list[$i]['filename'] = $filename; //文件名,包含扩展名 
  94.     $file_list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime($file)); //文件最后修改时间 
  95.     $i++; 
  96.    } 
  97.    closedir($handle); 
  98.   } 
  99. //排序 
  100.   usort($file_list, 'cmp_func'); 
  101.   $result = array(); 
  102. //相对于根目录的上一级目录 
  103.   $result['moveup_dir_path'] = $moveup_dir_path; 
  104. //相对于根目录的当前目录 
  105.   $result['current_dir_path'] = $current_dir_path; 
  106. //当前目录的URL 
  107.   $result['current_url'] = $current_url; 
  108. //文件数 
  109.   $result['total_count'] = count($file_list); 
  110. //文件列表数组 
  111.   $result['file_list'] = $file_list; 
  112. //输出JSON字符串 
  113.   header('Content-type: application/json; charset=UTF-8'); 
  114.   $json = new Services_JSON(); 
  115.   echo $json->encode($result); 
  116. }

Tags: thinkphp3 2上传图片

分享到: