php实现文件上传的例子(附详细源码)

今天用PHP做了个文件上传工具,功能还是很完善滴,如下:

每个图片上传之后,都有自己的地址,改变原图位置或重命名时,将不会重复上传。

一、功能:

A:文件分类上传

B:生成相应的文件夹,如本例,选择团,生成文件夹tuan

C:点击图片,查看详细

二、HTML代码:

	<div id="container">
		<fieldset>
			<legend class="img"><img width="100%" src="css/logo.png" /></legend>
			<form action="" method="post" name="myform" id="myform" οnsubmit="return false" enctype="multipart/form-data">
			    <strong>亲,请选择你要上传的文件</strong>
				<div class="file"><input type="file" id="fileToUpload" name="fileToUpload" /></div>
				<select id="product">
               		<option value="" <?php if(empty($pname)){ echo "selected" ;} ?> >请选择产品</option>
               		<option value="tuan" <?php if($pname == '2000tuan'){ echo "selected" ;} ?> >团</option>			
               	</select>
               	<button id="postBtn" >Submit</button>
			</form>
			<div id="info"></div>
			<div style="position:relative; top:40px; left:20px;">
				<a id="loading" style="display:none"><img width="10%" src="css/loading.gif" /></a>
			</div>
			<a href="#" id="img_link" target="_blank">
				<div id="img_url">buding</div>
				<br/>
				<img class="preview" id="preview" src="" style="display:none;margin:0 auto" />
			</a>
		</fieldset>
	</div>

三、Javascript代码:

	<script type="text/javascript">
		$('#postBtn').click(function(){
			$('#preview').hide();
			$('#img_url').hide();
			$('#loading')
				.ajaxStart(function(){
					$(this).show();
				})
				.ajaxComplete(function(){
					$(this).hide();
				});
			if($('#fileToUpload').val() == ""){
				$('#info').html("亲,还没选择文件呢");
				var jObject={"Url":"","Height":413};
				var jString=JSON.stringify(jObject);
				window.parent.postMessage(jString,'*');
				return false;
			} 
			if($('#product').val() == ""){
				$('#info').html("亲,还没选择产品呢");
				$('#info').css("color","#e9af32");
				var jObject={"Url":"","Height":413};
				var jString=JSON.stringify(jObject);
				window.parent.postMessage(jString,'*');
				return false;
			} 
			var val = $('#product').val();
			$.ajaxFileUpload({	
				url:'ajaxupload.php',
				secureuri:false,
				fileElementId:'fileToUpload',
				dataType: 'text',
				data:{product:val},
				success: function (data, status)
				{
					if(data.search(/http:\/\//i) < 0 ){
						$('#info').html(data);
						var jObject={"Url":"","Height":413};
						var jString=JSON.stringify(jObject);
						window.parent.postMessage(jString,'*');
					}else{
						$('#info').html("您上传的文件为:<br/>");
						$('#preview').attr("src",data);
						$('#img_link').attr("href",data);
						$('#img_url').html(data);
						$('#preview').show();
						$('#img_url').show();
						$('#preview').load(function(){
							var imgH=$('#preview').height();
							var jObject={"Url":data,"Height":imgH+228,"oid":"<?php echo @$_REQUEST['oid']; ?>"};
							var jString=JSON.stringify(jObject);
							window.parent.postMessage(jString,'*');
						});
					}
				},
				error: function (data, status, e){	
					$('#info').html(data+e);
				}
			});
		});
	</script>

四、PHP代码

<?php
require_once('config.php');
if(empty($_FILES) || empty($_REQUEST)){
	header('location:imgupload.php');
	exit;
}

array_push($_FILES, $_REQUEST);

$filename = 'fileToUpload';
$product = @$_FILES[0]['product'];
$today = date("Y-m-d");
$time = date("YmdHis"); 
$year = date("Y");
$month = date("m");
$day = date("d");
$img_path = $product.'/'.$year.'/'.$month.'/'.$day.'/';
$destination_dir = ROOT_PATH.'/pic/'.$img_path.'/';

if(!is_uploaded_file($_FILES[$filename]['tmp_name'])){//验证上传文件是否存在
	echo "请选择你想要上传的图片";
	exit;
}
	
if($product == "") {//选择产品
   	echo "请选择产品";
	exit;
}
	$files = $_FILES[$filename];
   
	if($max_file_size < $files['size']){//判断文件是否超过限制大小
		echo "图片太大了,传个小点的吧(<=2MB)";
		exit;
	}
		
	if(!file_exists($destination_dir)) {//判断上传目录是否存在,不存在则创建一个.
		if(!mkdir($destination_dir,0777,true)) {
			echo "创建目录 {".$destination_dir."} 失败<可能是权限问题>";
			exit;
		}
	}
	$type = pathinfo($files['name']);
    $type = strtolower($type["extension"]);
	$type =".".$type;
	$tmp_name = $files['tmp_name'];
	$md5file = md5_file($tmp_name);//生成md5文件
	$new_name =$md5file.$type;
    $img_relat_path = $img_path.$new_name;
	$img_abs_path =	$destination_dir.$new_name;
		
    $url = IMG_URL.$img_relat_path;

    //判断数据库中图片是否存在
    $sql="select url from file_url where md5 = '".$md5file."'";
    $res=$db->getOne($sql);
    if($res) {
        echo $res['url'];
        exit; 
    }      
   
    if(!move_uploaded_file ($files['tmp_name'], $img_abs_path)) {//上传文件
        echo "上传文件失败";
			exit;
    }
        //将图片存入数据库       
    $sql="insert into file_url(url,product,md5,create_time) values('".$url."','".$product."','".$md5file."','".$today."')";
    $db->Execute($sql);
    $db->CloseDB();
    echo $url;
?>



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值