Ajax 文件上传之PHP心得
生活随笔
收集整理的這篇文章主要介紹了
Ajax 文件上传之PHP心得
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?????? 最近發(fā)現(xiàn)網(wǎng)上轉(zhuǎn)載不注明出處的文章很多,為了創(chuàng)造一個(gè)良好的開源環(huán)境.請(qǐng)您轉(zhuǎn)載后注明出處.謝謝合作!這樣會(huì)鼓勵(lì)我們的開源欲望.
jquery 這個(gè)JS組件不知道大家用過沒有?在有一定的Ajax基礎(chǔ)之后,利用它來開發(fā)Ajax是一件非常有趣的事情,一直以來就被Js的傳統(tǒng)編程環(huán)境所煩的.它可以簡(jiǎn)化我的的JS 代碼,加快你們的開發(fā)速度,代碼一目了然.一但有了自己的編程習(xí)慣之后,那可以說會(huì)如魚得水.
今天利用Jquery 的File Upload 組件,再結(jié)合Php那簡(jiǎn)單的文件上傳組件,一個(gè)非常 高效的Ajax文件上傳工具就搞定啦.可以實(shí)現(xiàn)我們一般的程序要求,下面就和大家分享一下,免得和我一樣在網(wǎng)上找來找去都沒得一個(gè)實(shí)用的,最后還得自己寫一個(gè).~_~
效果圖如下:
JS代碼:
<script type="text/javascript" language="javascript">function ajaxFileUpload(){$("#loading").ajaxStart(function(){$(this).show(); }).ajaxComplete(function(){$(this).hide(); });$.ajaxFileUpload({url:'up.php',secureuri:false,fileElementId:'fileToUpload',dataType: 'html',success: function (data){ alert(data);},error: function (data, status, e){alert(e);}})return false;}</script> Php文件上件處理代碼: #--time:2008.07.10--##--anthor:fkedwgwy--##--QQ群:37304662--##--blog:http://blog.csdn.net/fkedwgwy--##--歡迎您加入PHP開源的殿堂.--#function uploadFile($file,$isImage=false){set_time_limit(0);$file_name=$file['name'];$file_type=$file['type'];$file_tmpname=$file['tmp_name'];$file_size=$file['size'];$file_error=$file['error'];if(!empty($file_name)){if($isImage==true){if ($file_error==UPLOAD_ERROR_OK) {if ($file_type=="image/gif"||$file_type=="image/jpg"||$file_type=="image/pjpeg"||$file_type=="image/jpeg"||$file_type=="image/x-png"){$upload_path="file/";$upload_name=strrchr($file_name,".");$upload_name=date("YmdHiss").$upload_name;$upload_path=$upload_path.$upload_name;if(move_uploaded_file($file_tmpname,$upload_path)){return $upload_path;}else { $msg="上傳失敗";}}else {$error="圖片格式不對(duì)";}}else {$error=$file_error;}}else {if ($file_error==UPLOAD_ERROR_OK) {$upload_path="file/";$upload_name=strrchr($file_name,".");$upload_name=date("YmdHis").$upload_name; $upload_path.=$upload_name;if(move_uploaded_file($file_tmpname,$upload_path)){return $upload_path;}else {$msg="上傳失敗";}} }}else {if($isImage==true){$error="請(qǐng)選擇你要上傳的圖片";}else {$error="請(qǐng)選擇你要上傳的軟件";}} } ? html部分: <table cellpadding="0" cellspacing="0" class="tableForm"><thead><tr><th>fkedwgwy-Ajax_Php_file_upload</th></tr></thead><tbody><tr><td><input id="fileToUpload" type="file" size="45" name="fileToUpload"></td></tr><tr><td>請(qǐng)選擇文件上傳(PHP家園QQ群:37304662)</td></tr></tbody><tfoot><tr><td><button class="button" id="buttonUpload" οnclick="return ajaxFileUpload();">Upload</button><a href="http://blog.csdn.net/fkedwgwy">http://blog.csdn.net/fkedwgwy</a></td></tr></tfoot></table>原代碼下載地址:http://download.csdn.net/source/533941 ? ?轉(zhuǎn)載于:https://www.cnblogs.com/fengju/archive/2008/07/10/6174048.html
總結(jié)
以上是生活随笔為你收集整理的Ajax 文件上传之PHP心得的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 震惊世界的中国秘方
- 下一篇: php生成静态html分页实现方法