Ajax 文件上传之PHP心得

本文介绍如何结合JQuery FileUpload组件与PHP实现高效稳定的Ajax文件上传功能。通过简化JavaScript代码并提供易于理解的示例,帮助读者快速掌握该技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近发现网上转载不注明出处的文章很多,为了创造一个良好的开源环境.请您转载后注明出处.谢谢合作!这样会鼓励我们的开源欲望.

jquery 这个JS组件不知道大家用过没有?在有一定的Ajax基础之后,利用它来开发Ajax是一件非常有趣的事情,一直以来就被Js的传统编程环境所烦的.它可以简化我的的JS 代码,加快你们的开发速度,代码一目了然.一但有了自己的编程习惯之后,那可以说会如鱼得水.

今天利用Jquery 的File Upload 组件,再结合Php那简单的文件上传组件,一个非常 高效的Ajax文件上传工具就搞定啦.可以实现我们一般的程序要求,下面就和大家分享一下,免得和我一样在网上找来找去都没得一个实用的,最后还得自己写一个.~_~

效果图如下:

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--#



#--:37304662--#



#--blog:http://blog.youkuaiyun.com/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="图片格式不对";

				}

			}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="请选择你要上传的图片";

		}else {

			$error="请选择你要上传的软件";

		}

		

	}	

}
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> 请选择文件上传(PHP家园:37304662)</td> </tr> </tbody> <tfoot> <tr> <td> <button class="button" id="buttonUpload" onclick="return ajaxFileUpload();"> Upload</button> <a href="http://blog.youkuaiyun.com/fkedwgwy">http://blog.youkuaiyun.com/fkedwgwy</a></td> </tr> </tfoot> </table> 原代码下载地址:http://download.youkuaiyun.com/source/533941
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值