日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

上传图片到php服务器

發布時間:2023/12/8 php 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 上传图片到php服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
html代碼
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>上傳圖片到服務器</title><script src="lib/jquery-1.8.3.min.js"></script><style>#box2 {width: 100px;height: 100px;background-color: aquamarine;position: absolute;}#box{height:200px;width:440px;border:2px solid red;}</style> </head> <body><div class="container"><input id="imagefile" type="file" name="filename" /><!-- ondragenter="drager()" --><img id="preview" alt="image" style="height: 200px;" name="pic" ondragover="allowDrop(event)" ondrop="drop1(event)"/></div><div id="box2"></div><script>function allowDrop(ev){ev.preventDefault();}var oDiv = document.getElementById("box2");oDiv.onmousedown=function(ev){var oEvent = ev; var disX = oEvent.clientX - oDiv.offsetLeft;//offsetLeft,offsetTop:相對于最近的祖先定位元素。var disY = oEvent.clientY - oDiv.offsetTop;document.onmousemove=function (ev){oEvent = ev;oDiv.style.left = oEvent.clientX -disX+"px";oDiv.style.top = oEvent.clientY -disY+"px";}document.onmouseup=function(){document.onmousemove=null;document.onmouseup=null;} }//點擊input標簽添加$("#imagefile").change(function() { if(typeof FileReader == 'undefined') {alert("抱歉,你的瀏覽器不支持FileReader,無法圖片上傳"); } var file = this.files[0]; postImg(file);});// 拖拽添加function drop1(ev){ev.preventDefault(); var fileList = ev.dataTransfer.files; //獲取文件對象if(fileList.length == 0){return false;}if(fileList[0].type.indexOf('image') === -1){alert('您拖的不是圖片!')return false;}postImg(fileList[0]);};// 封裝上傳function postImg(file){var reader = new FileReader(); reader.readAsDataURL(file); //dataurl的格式為data:image/******;adshgflkasnga,意思是文件頭+二進制串,可用;分割reader.onload = function(e) { $.post("./img.php",{ img: e.target.result }, //post成功后的回調函數function(ret) { if(ret.img != '') {var pic = document.getElementById("preview");pic.src = ret.img; } else { alert('upload fail'); }},'json');}}</script> </body> </html>

php代碼

$img1 = $_POST['img'];$img = $img1?$img1:'';list($type,$data) = explode(',',$img);if(strstr($type,'image/jpeg')!==''){$ext = ".jpg";}elseif(strstr($type,'image/gif')!==''){$ext = ".gif";}elseif(strstr($type,'image/png')!==''){$ext = ".png";}$photo = time().$ext;$photo_path = "img/{$photo}";file_put_contents($photo_path,base64_decode($data),true);header('content-type:application/json;charset=utf-8');$ret = array('img'=>$photo_path);echo json_encode($ret);

總結

以上是生活随笔為你收集整理的上传图片到php服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。