Commit 5932caa791767e7df7a2f512c5d570291e2e5212
1 parent
508e8023
【留言板】 文件上传实现
Showing
1 changed file
with
64 additions
and
30 deletions
trunk/Message/Api/Controller/Message/MessInputController.class.php
@@ -21,10 +21,8 @@ class MessInputController extends \Api\Controller\AbstractController | @@ -21,10 +21,8 @@ class MessInputController extends \Api\Controller\AbstractController | ||
21 | */ | 21 | */ |
22 | public function Index_post() | 22 | public function Index_post() |
23 | { | 23 | { |
24 | - | ||
25 | session_start(); | 24 | session_start(); |
26 | $data = $this->checkFields(I('post.')); | 25 | $data = $this->checkFields(I('post.')); |
27 | - | ||
28 | if (!$data) { | 26 | if (!$data) { |
29 | $this->_result = "数据验证失败"; | 27 | $this->_result = "数据验证失败"; |
30 | return false; | 28 | return false; |
@@ -37,61 +35,73 @@ class MessInputController extends \Api\Controller\AbstractController | @@ -37,61 +35,73 @@ class MessInputController extends \Api\Controller\AbstractController | ||
37 | $data['m_ip'] = $_SERVER['REMOTE_ADDR']; | 35 | $data['m_ip'] = $_SERVER['REMOTE_ADDR']; |
38 | $data['domain'] = 'wx5019b9c455ea9c81'; | 36 | $data['domain'] = 'wx5019b9c455ea9c81'; |
39 | 37 | ||
40 | - /* | ||
41 | - * 上传图片 取得图片路径 $path_img | ||
42 | - * $data['m_imgs'] = $path_img; | ||
43 | - * | ||
44 | - */ | ||
45 | - if ($_FILES['myImg']) { | ||
46 | - // coding... | ||
47 | - } | ||
48 | 38 | ||
49 | - $mid = $messagesModel->insert($data); | 39 | + // 上传图片 取得图片路径 $path_img |
40 | + if ($_FILES['myImg']['error'] == 0) { | ||
41 | + $imgPath = $this->uploadFiles($_FILES['myImg'],$data['m_uid']); | ||
42 | + if ($imgPath === false) { | ||
43 | + return false; | ||
44 | + } else { | ||
45 | + $data['m_imgs'] = $imgPath; | ||
46 | + } | ||
47 | + } | ||
50 | 48 | ||
51 | - /* | ||
52 | - if (($mid = $messagesModel->insert($data)) != null && $_FILES['myFile']) { | 49 | + if (($mid = $messagesModel->insert($data)) != null && $_FILES['myFile']['error'] == 0) { |
50 | + $att_flg = true; | ||
53 | // 留言成功后开始上传附件 | 51 | // 留言成功后开始上传附件 |
54 | $attachmentModel = new AttachmentModel(); | 52 | $attachmentModel = new AttachmentModel(); |
55 | unset($data); | 53 | unset($data); |
56 | $data['a_mid'] = $mid; | 54 | $data['a_mid'] = $mid; |
55 | + $data['domain'] = 'wx5019b9c455ea9c81'; | ||
57 | 56 | ||
58 | - | ||
59 | - // 附件上传实现 取得附件路径 $path_att | ||
60 | - // $data['a_path'] = $path_att; | ||
61 | - // | 57 | + $attPath = $this->uploadFiles($_FILES['myFile'],$mid); |
58 | + if ($attPath === false) { | ||
59 | + return false; | ||
60 | + } else { | ||
61 | + $data['a_path'] = $attPath; | ||
62 | + } | ||
63 | + // 附件上传成功 取得附件路径 $attPath | ||
62 | if (($aid = $attachmentModel->insert($data)) != null) { | 64 | if (($aid = $attachmentModel->insert($data)) != null) { |
63 | $this->_result = "留言成功,上传附件成功。"; | 65 | $this->_result = "留言成功,上传附件成功。"; |
64 | }else{ | 66 | }else{ |
65 | // 上传附件失败后 进行相应的处理 | 67 | // 上传附件失败后 进行相应的处理 |
66 | $this->_result = "上传附件失败。"; | 68 | $this->_result = "上传附件失败。"; |
67 | } | 69 | } |
70 | + } | ||
68 | 71 | ||
69 | - }*/ | ||
70 | - | ||
71 | - if ($mid > 0) { | 72 | + if ($mid && $att_flg) { |
73 | + if ($aid > 0) { | ||
74 | + $this->_result = [ | ||
75 | + 'mid' => $mid, | ||
76 | + 'aid' => $aid | ||
77 | + ]; | ||
78 | + } else { | ||
79 | + $messagesModel->update_mess($mid,'del'); | ||
80 | + echo '<script type="text/javascript">alert("附件上传失败。");</script>'; | ||
81 | + return false; | ||
82 | + } | ||
83 | + } else if ($mid && !$att_flg) { | ||
72 | $this->_result = [ | 84 | $this->_result = [ |
73 | - 'mid' => $mid | 85 | + 'mid' =>$mid |
74 | ]; | 86 | ]; |
75 | - header('Location: http://lys.com/mess_front/list.php'); | ||
76 | - } else { | ||
77 | - $this->_result = $mid; | 87 | + } else if(!$mid){ |
88 | + echo '<script type="text/javascript">alert("留言失败。");</script>'; | ||
89 | + $this->_result = '留言失败。'; | ||
90 | + return false; | ||
78 | } | 91 | } |
79 | 92 | ||
80 | - | ||
81 | - | 93 | + header('Location: http://lys.com/mess_front/list.php'); |
82 | } | 94 | } |
83 | - | ||
84 | /** | 95 | /** |
85 | * 验证提交参数字段合法性 | 96 | * 验证提交参数字段合法性 |
86 | * | 97 | * |
87 | - * @param array $postData | 98 | + * @param array $postData:post提交的数据 |
88 | * @return bool | 99 | * @return bool |
89 | */ | 100 | */ |
90 | private function checkFields($postData = []) | 101 | private function checkFields($postData = []) |
91 | { | 102 | { |
92 | $parrent = "/\ |\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\\' | \`|\-|\=|\\\|\|/"; | 103 | $parrent = "/\ |\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\\' | \`|\-|\=|\\\|\|/"; |
93 | $data = []; | 104 | $data = []; |
94 | - | ||
95 | if (empty($postData['title']) || strlen($postData['title'])>80) { | 105 | if (empty($postData['title']) || strlen($postData['title'])>80) { |
96 | $this->_result = "标题不能为空且标题长度必须小于80。"; | 106 | $this->_result = "标题不能为空且标题长度必须小于80。"; |
97 | return false; | 107 | return false; |
@@ -106,7 +116,31 @@ class MessInputController extends \Api\Controller\AbstractController | @@ -106,7 +116,31 @@ class MessInputController extends \Api\Controller\AbstractController | ||
106 | } else { | 116 | } else { |
107 | $data['message'] = preg_replace($parrent, "", $postData['message']); | 117 | $data['message'] = preg_replace($parrent, "", $postData['message']); |
108 | } | 118 | } |
109 | - | ||
110 | return $data; | 119 | return $data; |
111 | } | 120 | } |
121 | + /** | ||
122 | + * 文件上传功能 | ||
123 | + * @param null $file:表单提交文件 | ||
124 | + * @param null $mid:留言ID(上传附件时需要) | ||
125 | + * @return bool|string:上传成功时返回文件路径 | ||
126 | + */ | ||
127 | + protected function uploadFiles($file = null,$mid = null) | ||
128 | + { | ||
129 | + $path = $_SERVER['DOCUMENT_ROOT']; | ||
130 | + $fileInfo = $file; | ||
131 | + if ($fileInfo['error'] === 0 && $fileInfo['size']<(2048*1000)) { | ||
132 | + $fileType = end(explode('.', $fileInfo['name'])); | ||
133 | + $filePath = '/static/message/'.$mid.'_'.time().'.'.$fileType; | ||
134 | + if (move_uploaded_file($fileInfo['tmp_name'],$path.$filePath)) { | ||
135 | + return $filePath; | ||
136 | + } else { | ||
137 | + $this->_result = '移动文件失败。'; | ||
138 | + return false; | ||
139 | + } | ||
140 | + } else { | ||
141 | + $this->_result = '上传失败。'; | ||
142 | + return false; | ||
143 | + } | ||
144 | + } | ||
112 | } | 145 | } |
146 | + |