php+iframe 實(shí)現(xiàn)上傳文件功能示例
本文實(shí)例講述了php+iframe 實(shí)現(xiàn)上傳文件功能。分享給大家供大家參考,具體如下:
我們通過動(dòng)態(tài)的創(chuàng)建iframe,修改form的target,來實(shí)現(xiàn)無跳轉(zhuǎn)的文件上傳。
具體的實(shí)現(xiàn)步驟
1.捕捉表單提交事件2.創(chuàng)建一個(gè)iframe3.修改表單的target,指向iframe4.刪除iframe
file.html代碼如下:
<!DOCTYPE HTML><html lang='zh-CN'><head> <meta charset='UTF-8'> <title></title></head><body> <form action='upfile.php' method='post' enctype='multipart/form-data'> <input type='file' name='file' /> <input type='submit' name='submit' value='上傳' /> </form> <div id='result'></div></body><script type='text/javascript' src='http://www.baoyu77737.com/bcjs/jquery.js'></script><script type='text/javascript'>$('#upfileForm').bind('submit', function() { var ifrName = ’upfile’ + Math.random(); var ifr = $('<iframe name=’' + ifrName + '’ width=’0’ height=’0’ frameborder=’0’></iframe>'); $('body').append(ifr); $(this).attr('target', ifrName);});</script></html>
upfile.php代碼如下:
<?php$uploadDir = ’./upload/’;if(!file_exists($uploadDir)) { @mkdir($uploadDir, 0777, true);}$uploadFile = $uploadDir . basename($_FILES[’file’][’name’]);if(move_uploaded_file($_FILES[’file’][’tmp_name’], $uploadFile)) { echo '<script>parent.document.getElementById(’result’).innerHTML=’OK’;</script>';} else { echo '<script>parent.document.getElementById(’result’).innerHTML=’NO’;</script>';}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP目錄操作技巧匯總》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》及《PHP網(wǎng)絡(luò)編程技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. 解決Python中報(bào)錯(cuò)TypeError: must be str, not bytes問題2. Python如何解決secure_filename對中文不支持問題3. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考4. ASP基礎(chǔ)入門第二篇(ASP基礎(chǔ)知識)5. 不使用XMLHttpRequest對象實(shí)現(xiàn)Ajax效果的方法小結(jié)6. Python類成員繼承重寫的實(shí)現(xiàn)7. 使用python tkinter開發(fā)一個(gè)爬取B站直播彈幕工具的實(shí)現(xiàn)代碼8. Python 用NumPy創(chuàng)建二維數(shù)組的案例9. ThinkPHP6使用JWT+中間件實(shí)現(xiàn)Token驗(yàn)證實(shí)例詳解10. python安裝sklearn模塊的方法詳解
