In fact, upload files understand the reason is very simple, small series at that time is also confused, make a lot of genius to understand a little, good nonsense less say, hey. The first thing to use is the tp3.2 frame, and here I have the front name img.html.
So the background first use the Public function img (), and then select the type of product to upload, and then select the fixed function according to the type, and upload is to determine the format of the uploaded file and save the path. Finally, upload to the database according to DB_IMG.
First, the front-end code
<! DOCTYPE html>
Next is the backstage (using the tp3.2 framework)
Add a picture in the background
Public function upload () {////Instantiate upload class $upload = new \think\upload (); $upload->maxsize = 3145728; $upload->exts = [' jpg ', ' jpeg ', ' gif ', ' PNG ']; $upload->savepath = './public/uploads/'; $upload->rootpath = './'; $info = $upload->upload (); $upload = new Upload (); if (! $info) {$this->error ($upload->geterror ()); $this->error (' Shuangchuanshibai ', U (' Login/loginss '), 2); } else {//$this->success (' upload succeeded '); return $info; }}public function Insert () {$login = D (' logins '); $result = $login->where ([' username ' = $_sessin[' username '])->field ()->find (); if (empty ($_files)) {if (empty ($_files)) {$this->error (' Please select a file to upload '); } if (!empty ($_files)) {$arr = $this->upload (); foreach ($arr as $key = + $value) {$path [] [' savepath '] = $value [' Savepath ']. $value [' Savename ']; }//$data [' Single ']= ' recommended '; $data [' f_name '] = $_post[' f_name '); $data [' Create_time ']=time (); $data [' money '] = $_post[' money ']; $data [' message '] = $_post[' message ']; $data [' username '] = $_session[' username '); $data [' create_time '] = time//$data [' login_id '] = $result [' id '];(); if (Isset ($data)) {//If the information of the uploaded file is not empty, we will save this information to the database $this->db_img ($data, $path); } else {$this->error (' data is null '); }}}public function Db_img ($data, $path) {//if ($_post[' sel ']== ' close ') {//if ($_post[' single ']== ' Y ') {$file = D (' file '); $img = D (' File_c '); foreach ($path as $vo) {$data [' savepath '] = $vo [' Savepath ']; }//$data [' single '] = "recommended"; $data [' f_name '] = $_post[' f_name '); $data [' username '] = $_session[' username '); $t = time (); $data [' create_time '] = Date ("y-m-d h:i:s", $t); $data [' money '] = $_post[' money ']; $data [' message '] = $_post[' message ']; $img->add (); if (! $img->create ($data) | |! $file->create ($data)) {$this->error (' upload failed '); } else {if (!$Img->add ($data) | | ! $file->add ($data)) {$this->error (' upload failed '); }} $this->success (' Upload successful ... ', U (' login/img '));} Insert1public function Insert1 () {$login = D (' logins '); $result = $login->where ([' username ' = $_sessin[' username '])->field ()->find (); if (empty ($_files)) {if (empty ($_files)) {$this->error (' Please select a file to upload '); } if (!empty ($_files)) {$arr = $this->upload (); foreach ($arr as $key = + $value) {$path [] [' savepath '] = $value [' Savepath ']. $value [' Savename ']; } $data [' f_name '] = $_post[' f_name ']; $data [' money '] = $_post[' money ']; $data [' message '] = $_post[' message ']; $data [' username '] = $_session[' username '); if (Isset ($data)) {//If the information of the uploaded file is not empty, we will save this information to the database $this->db_img1 ($data, $path); } else {$this->error (' data is null '); }}}public function Db_img1 ($data, $path) {//if ($_post[' sel ']== ' close ') {//if ($_post[' single ']== ' Y ') {$img = D (' File_b '); $File = D (' file '); foreach ($path as $vo) {$data [' savepath '] = $vo [' Savepath ']; } $data [' f_name '] = $_post[' f_name ']; $data [' username '] = $_session[' username '); $t = time (); $data [' create_time '] = Date ("y-m-d h:i:s", $t); $data [' money '] = $_post[' money ']; $data [' message '] = $_post[' message ']; $img->add ($data); if (! $img->create ($data) | |! $file->create ($data)) {$this->error (' upload failed '); } else {if (! $img->add ($data) | |! $file->add ($data)) {$this->error (' upload failed '); }} $this->success (' Upload successful ... ', U (' login/img '));} Insert2public function Insert2 () {$login = D (' logins '); $result = $login->where ([' username ' = $_sessin[' username '])->field ()->find (); if (empty ($_files)) {if (empty ($_files)) {$this->error (' Please select a file to upload '); } if (!empty ($_files)) {$arr = $this->upload (); foreach ($arr as $key = + $value) {$path [] [' savepath '] = $value [' Savepath ']. $value [' SavenaMe ']; } $data [' f_name '] = $_post[' f_name ']; $data [' money '] = $_post[' money ']; $data [' message '] = $_post[' message ']; $data [' username '] = $_session[' username '); if (Isset ($data)) {//If the information of the uploaded file is not empty, we will save this information to the database $this->db_img2 ($data, $path); } else {$this->error (' data is null '); }}}public function Db_img2 ($data, $path) {//if ($_post[' sel ']== ' close ') {//if ($_post[' single ']== ' Y ') {$img = D (' file_d '); $file = D (' file '); foreach ($path as $vo) {$data [' savepath '] = $vo [' Savepath ']; } $data [' f_name '] = $_post[' f_name ']; $data [' username '] = $_session[' username '); $t = time (); $data [' create_time '] = Date ("y-m-d h:i:s", $t); $data [' money '] = $_post[' money ']; $data [' message '] = $_post[' message ']; $img->add ($data); if (! $img->create ($data) | |! $file->create ($data)) {$this->error (' upload failed '); } else {if (! $img->add ($data) | |! $file->add ($data)) {$thiS->error (' upload failed '); }} $this->success (' Upload successful ... ', U (' login/img '));} Public function img () {if (is_post) {if ($_post[' sel '] = = ' close ') {$this->insert (); } if ($_post[' sel '] = = ' book ') {$this->insert1 (); } if ($_post[' sel '] = = ' Dian ') {$this->insert2 (); }} $this->display ();}
PHP article upload file