css li 超出隱藏代碼
667
2024-03-30
本例子中沒有采用File Post上傳文件方式!原理一樣,為了更加的理解base64 選擇將其輸出在文本域中,并提交至服務(wù)器!運(yùn)用到項(xiàng)目中建議采用提交File方式。
html代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>簡單的html5 File測試 for pic2base64</title> <style> </style> <script> window.onload = function(){ var input = document.getElementById("demo_input"); var result= document.getElementById("result"); var img_area = document.getElementById("img_area"); if ( typeof(FileReader) === 'undefined' ){ result.innerHTML = "抱歉,你的瀏覽器不支持 FileReader,請使用現(xiàn)代瀏覽器操作!"; input.setAttribute('disabled','disabled'); }else{ input.addEventListener('change',readFile,false); } } function readFile(){ var file = this.files[0]; //這里我們判斷下類型如果不是圖片就返回 去掉就可以上傳任意文件 if(!/image/w+/.test(file.type)){ alert("請確保文件為圖像類型"); return false; } var reader = new FileReader(); reader.readAsDataURL(file); console.log(); reader.onload = function(e){ result.innerHTML = this.result; img_area.innerHTML = '<div class="sitetip">圖片img標(biāo)簽展示:</div>'; } } </script> </head> <body> <form action="file.php" method="post"> <input type="file" value="sdgsdg" id="demo_input" /> <textarea name="img" id="result" rows=30 cols=300></textarea> <p id="img_area"></p> <input type="submit" value="提交"> </form> </body> </html>
PHP功能塊代碼
<?php /** * base64圖片上傳 * @param $base64_img * @return array */ $base64_img = trim($_POST['img']); $up_dir = './upload/';//存放在當(dāng)前目錄的upload文件夾下 if(!file_exists($up_dir)){ mkdir($up_dir,0777); } if(preg_match('/^(data:s*image/(w+);base64,)/', $base64_img, $result)){ $type = $result[2]; if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){ $new_file = $up_dir.date('YmdHis_').'.'.$type; if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){ $img_path = str_replace('../../..', '', $new_file); echo '圖片上傳成功</br>'; }else{ echo '圖片上傳失敗</br>'; } }else{ //文件類型錯誤 echo '圖片上傳類型錯誤'; } }else{ //文件錯誤 echo '文件錯誤'; }
實(shí)例效果如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持綠夏網(wǎng)。
#免責(zé)聲明#
本站[綠夏技術(shù)導(dǎo)航]提供的一切軟件、教程和內(nèi)容信息僅限用于學(xué)習(xí)和研究目的;不得將上述內(nèi)容用于商業(yè)或者非法用途,否則,一切后果請用戶自負(fù)。本站信息來自網(wǎng)絡(luò)收集整理,版權(quán)爭議與本站無關(guān)。您必須在下載后的24個小時之內(nèi),從您的電腦或手機(jī)中徹底刪除上述內(nèi)容。如果您喜歡該程序或內(nèi)容,請支持正版,購買注冊,得到更好的正版服務(wù)。我們非常重視版權(quán)問題,如有侵權(quán)請郵件[admin@lxwl520.com]與我們聯(lián)系進(jìn)行刪除處理。敬請諒解!