当前位置:Gxlcms > PHP教程 > thinkphp3.2实现上传图片的控制器方法_PHP

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

时间:2021-07-01 10:21:17 帮助过:49人阅读

本文讲述了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字符串 header('Content-type: application/json; charset=UTF-8'); $json = new Services_JSON(); echo $json->encode($result); }

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《smarty模板入门基础教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

人气教程排行