一、前端
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <title>上传图片</title> <style> button{cursor: pointer;height: 30px;} .item-line{display: flex;flex-wrap: wrap;} </style> </head> <body> <div class="weui-cells weui-cells_form"> <form action="HandleImg.php" method="post" enctype="multipart/form-data"> <div class="body"> <div class="item"> <div class="item-line"><p>图片1:</p> <p><input type="file" name="img1" ></p></div> <div class="item-line"><p>图片2:</p> <p><input type="file" name="img2" ></p></div> </div> <div class="but-body"> <button type="submit">提交上传</button> <a href="/">取消</a> </div> </div> </form> </div> </body> </html>
二、处理里图片
<?php if(isGet()){ header("location:/"); } include \'service/UploadService.php\'; header(\'Content-Type:text/html; charset=utf-8;\'); $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = \'jiacheng\'; //$servername = "156.232.241.211"; //$username = "a240386333"; //$password = "chen123"; //$dbname = "a240386333"; $conn = mysqli_connect($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } if(isset($_FILES) && !empty($_FILES)){ $file=$_FILES; // print_r($file); $data=[\'url1\'=>\'\',\'url2\'=>\'\']; if($file[\'img1\'][\'error\']==4 && $file[\'img2\'][\'error\']==4){ jump(); die; } if($file[\'img1\'][\'error\']==0){ $res=service\UploadService::upload($file[\'img1\']); if($res[\'code\']==0){ $data[\'url1\']=$res[\'data\'][\'url\']; } } if($file[\'img2\'][\'error\']==0){ $res=service\UploadService::upload($file[\'img2\']); // echo \'$res\'; // print_r($res); if($res[\'code\']==0){ $data[\'url2\']=$res[\'data\'][\'url\']; } } // echo \'data:\'; // print_r($data); update($conn,$data); die; } jump(); function update($conn,$data){ if($data[\'url1\']==\'\'&&$data[\'url2\']==\'\'){ jump(\'上传失败\'); die; } else if($data[\'url1\']!=\'\'&&$data[\'url2\']!=\'\'){ $updatesql="UPDATE chen set u1=\'".$data[\'url1\']."\' , u2=\'".$data[\'url2\']."\' where id=0"; } else if($data[\'url1\']!=\'\'&&$data[\'url2\']==\'\'){ $updatesql="UPDATE chen set u1=\'".$data[\'url1\']."\' where id=0"; } else if($data[\'url1\']==\'\'&&$data[\'url2\']!=\'\'){ $updatesql="UPDATE chen set u2=\'".$data[\'url2\']."\' where id=0"; } $retval=mysqli_query($conn,$updatesql); if(!$retval) { jump(\'更新失败\',1); die; } $conn->close(); jump(\'更新成功\',0); die; } function jump($msg=\'请选择图片\',$code=1){ $url=$code==1?\'/upload.html\':\'/upload.html\'; $js="<script> alert(\'{$msg}\'); setTimeout(function(){ location.href=\'{$url}\'; },1500) </script>"; echo $js; } //是否是GET提交 function isGet(){ return $_SERVER[\'REQUEST_METHOD\'] == \'GET\' ? true : false; }
三、七牛上传核心文件
sdk地址 : https://github.com/qiniu/php-sdk
/** * 上传资源 * @param $file * @return array * @throws \Exception */ public static function upload($file){ try{ $accessKey = \'\';//AK $secretKey = \'\';//SK $qn_domain=\'\';//绑定域名 $auth=new Auth($accessKey,$secretKey); $bucket=\'\';//存储空间 $token = $auth->uploadToken($bucket); $uploadMgr = new \Qiniu\Storage\UploadManager(); $filePath = $file[\'tmp_name\'];//\'./php-logo.png\'; //接收图片信息 if($file[\'type\']==\'video/mp4\'){ $key = \'video\'.time().\'.mp4\'; }elseif($file[\'type\']==\'audio/mp3\'){ $key = \'audio\'.time().\'.mp3\'; }else{ $key = \'png\'.time().\'.png\'; } list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath); if($err!=null){ return ["code"=> 1,"msg"=> $err]; } else { //返回图片的完整URL $url ="http://{$qn_domain}/".$ret[\'key\']; $data=[\'url\'=>$url]; return [\'code\'=>0,\'msg\'=>\'上传成功\',\'data\'=>$data]; } }catch (\Exception $e){ return ["code"=> 1,"msg"=> $e->getMessage()]; } }