哪位大神有添加個人資訊中上傳照片,並將其儲存在資料庫中的原始碼,能發給我一份嗎?
shao_dmhwl@163.com
回複討論(解決方案)
給你個上傳類吧 new後直接調用upFile(),注意檔案要base64,然後調用getFileInfo()擷取資訊 儲存入庫即可 檔案格式你可以自己擴充下 我這個只支援jpg
"檔案大小超出 post_max_size 限制" , "SIZE" => "檔案大小超出網站限制" , "TYPE" => "不允許的檔案類型" , "DIR" => "目錄建立失敗" , "IO" => "輸入輸出錯誤" , "UNKNOWN" => "未知錯誤" , "MOVE" => "檔案儲存時出錯" ); /** * 建構函式 * @param string $fileField 表單名稱 * @param array $config 配置項 * @param bool $base64 是否解析base64編碼,可省略。若開啟,則$fileField代表的是base64編碼的字串表單名 */ public function __construct( $fileField , $config , $base64 = false ) { $this->fileField = $fileField; $this->config = $config; $this->stateInfo = $this->stateMap[ 0 ]; $this->upFile( $base64 ); } /** * 上傳檔案的主處理方法 * @param $base64 * @return mixed */ private function upFile( $base64 ) { //處理base64上傳 if ( "base64" == $base64 ) { $content = $_POST[ $this->fileField ]; $this->base64ToImage( $content ); return; } //處理普通上傳 $file = $this->file = $_FILES[ $this->fileField ]; if ( !$file ) { $this->stateInfo = $this->getStateInfo( 'POST' ); return; } if ( $this->file[ 'error' ] ) { $this->stateInfo = $this->getStateInfo( $file[ 'error' ] ); return; } if ( !is_uploaded_file( $file[ 'tmp_name' ] ) ) { $this->stateInfo = $this->getStateInfo( "UNKNOWN" ); return; } $this->oriName = $file[ 'name' ]; $this->fileSize = $file[ 'size' ]; $this->fileType = $this->getFileExt(); if ( !$this->checkSize() ) { $this->stateInfo = $this->getStateInfo( "SIZE" ); return; } if ( !$this->checkType() ) { $this->stateInfo = $this->getStateInfo( "TYPE" ); return; } if ( $this->stateInfo == $this->stateMap[ 0 ]){ $ret = \Biz\Common::savePic($file["tmp_name"]); $this->saveName = \Biz\Common::getPicUrl($ret); if (!$ret) { $this->stateInfo = $this->getStateInfo( "MOVE" ); } } } /** * 處理base64編碼的圖片上傳 * @param $base64Data * @return mixed */ private function base64ToImage( $base64Data) { $img = base64_decode( $base64Data ); $this->fileName = time() . rand( 1 , 10000 ) . ".png"; $this->fullName = $this->getFolder() . '/' . $this->fileName; if ( !file_put_contents( $this->fullName , $img ) ) { $this->stateInfo = $this->getStateInfo( "IO" ); return; } $this->oriName = ""; $this->fileSize = strlen( $img ); $this->fileType = ".png"; } /** * 擷取當前上傳成功檔案的各項資訊 * @return array */ public function getFileInfo() { return array( "originalName" => $this->oriName , "name" => $this->fileName , "url" => $this->saveName , "size" => $this->fileSize , "type" => $this->fileType , "state" => $this->stateInfo ); } /** * 上傳錯誤檢查 * @param $errCode * @return string */ private function getStateInfo( $errCode ) { return !$this->stateMap[ $errCode ] ? $this->stateMap[ "UNKNOWN" ] : $this->stateMap[ $errCode ]; } /** * 重新命名檔案 * @return string */ private function getName() { return md5(time().rand(1,10000).$this->file[ "name" ]).'.jpg'; } /** * 檔案類型檢測 * @return bool */ private function checkType() { return in_array( $this->getFileExt() , $this->config[ "allowFiles" ] ); } /** * 檔案大小檢測 * @return bool */ private function checkSize() { return $this->fileSize <= ( $this->config[ "maxSize" ] * 1024 ); } /** * 擷取副檔名 * @return string */ private function getFileExt() { return strtolower( strrchr( $this->file[ "name" ] , '.' ) ); }}