時(shí)間:2023-06-09 08:57:02 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-06-09 08:57:02 來(lái)源:網(wǎng)站運(yùn)營(yíng)
微擎模塊開(kāi)發(fā)之FTP上傳模塊:manifest.xml
文件,在<bindings></bindings>節(jié)點(diǎn)中新增一個(gè)節(jié)點(diǎn),代碼如下:<bindings> <menu> <entry title="立即上傳" do="do" state="" direct="false" /> </menu></bindings>
保存,上傳,預(yù)覽:<?php/** * 微站頁(yè)面 * @author chenyanphp@qq.com */defined('IN_IA') or exit('Access Denied');class Root_FtpModuleSite extends WeModuleSite{ /** * Ftp具體上傳操作 */ public function doWebDo() { // 模板 include $this->template("ftp_do"); }}
ftp_do代碼:{template 'common/header'}hello{template 'common/footer'}
重新安裝,查看頁(yè)面:{template 'common/header'}<form method="post" action="" enctype="multipart/form-data"> <div class="form-group"> <input id="thefile" name="thefile" type="file" style="display:none"> <div class="input-append"> <div class="col-xs-4"> <input id="file_name" class="form-control" type="text" style="height:30px;"> </div> <a class="btn btn-primary" onclick="$('input[id=thefile]').click();">瀏覽文件</a> </div> </div> <input type="submit" class="btn btn-success" name="sub" value="立即上傳"></form><script type="text/javascript"> $('input[id=thefile]').change(function() { $('#file_name').val($(this).val()); });</script>{template 'common/footer'}
然后site.php代碼就簡(jiǎn)單了,限制某些參數(shù),實(shí)現(xiàn)上傳:<?php/** * 微站頁(yè)面 * @author chenyanphp@qq.com */defined('IN_IA') or exit('Access Denied');class Root_FtpModuleSite extends WeModuleSite{ private $_max_size = 20; // 最大20字節(jié),實(shí)際微信MP_*.txt 只有16字節(jié) private $_file_type = 'txt'; // 只允許上傳txt文件 private $_error_code = [ // 錯(cuò)誤碼 0 => '沒(méi)有錯(cuò)誤發(fā)生,文件上傳成功', 1 => '上傳的文件超過(guò)了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值', 2 => '上傳文件的大小超過(guò)了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值', 3 => '文件只有部分被上傳', 4 => '沒(méi)有文件被上傳' ]; /** * Ftp具體上傳操作 */ public function doWebDo() { if ($_POST['sub']) { $file = $_FILES['thefile']; if ($file) { // 若所有檢測(cè)都通過(guò) if ($this->_checkError($file['error']) && $this->_checkMaxSize($file['size']) && $this->_checkFileType(pathinfo($file['name'])) && $this->_chekFileIsExists($file['name'])) { if (move_uploaded_file($file['tmp_name'], IA_ROOT.'/'.$file['name'])) { echo '<script>alert("上傳成功!")</script>'; } else { echo '<script>alert("上傳失敗!")</script>'; } } } } include $this->template("ftp_do"); } /** * 檢測(cè)文件上傳是否出錯(cuò) * * @param $error_code * @return bool */ private function _checkError($error_code) { if ($error_code > 0) { echo '<script>alert("'.$this->_error_code[$error_code].'");</script>'; return false; } return true; } /** * 檢測(cè)文件大小是否符合要求 * * @param $size * @return bool */ private function _checkMaxSize($size) { if ($size > $this->_max_size) { echo '<script>alert("上傳文件大小必須在20字節(jié)以?xún)?nèi)!");</script>'; return false; } return true; } /** * 檢測(cè)文件格式是否符合要求 * * @param $type * @return bool */ private function _checkFileType($type) { if ($type['extension'] != $this->_file_type) { echo '<script>alert("上傳文件格式必須為txt!");</script>'; return false; } return true; } /** * 檢測(cè)文件是否已存在 * * @param $filename * @return bool */ private function _chekFileIsExists($filename) { if (file_exists('/'.$filename)) { echo '<script>alert("文件已存在,請(qǐng)勿再次上傳!");</script>'; return false; } return true; }}
代碼完成,開(kāi)始操作:關(guān)鍵詞:
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。