首页 > php图像处理 > php 图片上传并预览效果

php 图片上传并预览效果

本文章是一款图片上传代码,他可以把上传到服务器的图片在进行预览修改,如果是你想的你就可以保存了,不是可以删除再重新上传,实例代码如下:

if(!isset($_session))session_start(); 
	 
	/**2010-6-22 
	 * $data 数组类型    包含以下变量 
	 ------------------------------- 
	 * $sub_type        submit类型(upload:上传按钮    delete:删除按钮),默认upload 
	 * $file            通过表单获取的$_files['filename']数组 
	 * $img_tag_id        预览图片的<img>的id值 
	 * $img_dir            上传图片的目录 
	 * $max_file_size    单位m(兆),默认:1m 
	 * $type_array        允许的上传的图片类型(默认:image/pjpeg、image/jpeg、image/gif) 
	 **/ 
	function upload($data){ 
	    if(!$data['file']){ 
	        echo '<script>alert("file不能为空!");</script>';exit; 
	    } 
	    if(!$data['img_tag_id']){ 
	        echo '<script>alert("预览图片标签id不能为空!");</script>';exit; 
	    } 
	    if(!$data['img_dir']){ 
	        echo '<script>alert("图片上传目录不能为空!");</script>';exit; 
	    } 
	    if(!isset($data['max_file_size'])){ 
	        $data['max_file_size'] = 1024 * 1024; 
	    }else{ 
	        $data['max_file_size'] = $data['max_file_size'] * 1024 * 1024; 
	    } 
	    if(!isset($data['type_array'])){ 
	        $data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif'); 
	    } 
	    if(!isset($data['sub_type'])){ 
	        $data['sub_type'] = 'upload'; 
	    } 
	 
	    $imgpath = ''; 
	    if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){ 
	        if(isset($_session['name']) && $_session['name']){ 
	            if(is_file($_session['imgpath'])){ 
	                $b = unlink($_session['imgpath']); 
	            } 
	            unset($_session['name'], $_session['imgpath']); 
	            if(!isset($_session['name'])){ 
	                echo '<script>alert("删除成功!");</script>'; 
	                echo '<script>parent.document.getelementbyid("'.$data['img_tag_id'].'").style.display = "none";</script>'; 
	            }else{ 
	                echo '<script>alert("删除失败!");</script>'; 
	            } 
	        }else{ 
	            echo '<script>alert("没有稿件!");</script>'; 
	        }exit; 
	    } 
	 
	    if(isset($_session['imgpath']) && $_session['imgpath']){ 
	        echo '<script>alert("稿件已经存在,要想重新上传请删除原来的稿件!");</script>';exit; 
	    } 
	    if(!in_array($data['file']['type'], $data['type_array'])){ 
	        echo '<script>alert("稿件类型不匹配,请上传.jpg、.gif和.png格式的图片!");</script>';exit; 
	    } 
	    if($data['file']['size'] > $data['max_file_size']){ 
	        echo '<script>alert("您上传的稿件过大,请选择2m以下的图片上传!");</script>';exit; 
	    } 
	 
	    if(!is_dir($data['img_dir'])){ 
	        @mkdir($data['img_dir'], 0777, true); 
	    } 
	    $imgpath    = $data['img_dir'].'/'.date('his', time()).rand(100, 999).$data['file']['name']; 
	    $isupload    = move_uploaded_file($data['file']['tmp_name'], $imgpath); 
	    if(!$isupload){ 
	        echo '<script>alert("稿件上传失败,请尝试重新上传!");</script>';exit; 
	    }else{ 
	        echo '<script>alert("稿件上传成功!");</script>'; 
	    } 
	    $_session['name']        = $data['file']['name']; 
	    $_session['imgpath']    = $imgpath; 
	 
	    return $imgpath; 
	} 
	 
	/*test_start*/ 
	$sub_type = ''; 
	if(isset($_post['submit_upload']))$sub_type = 'upload'; 
	if(isset($_post['submit_delete']))$sub_type = 'delete'; 
	if($sub_type){//echo '<script>alert("'.$sub_type.'");</script>';exit; 
	    $data = array(    'sub_type'        => $sub_type, 
	                    'file'            => $_files['file'], 
	                    'img_tag_id'    => 'picview', 
	                    'img_dir'        => 'upload_img', 
	                  ); 
	    $imgpath = upload($data); 
	}else{ 
	    $imgpath = isset($_session['imgpath'])? $_session['imgpath']: ''; 
	} 
	 
	 
	<form action="ad.php" method="post" enctype="multipart/form-data" target="frame"> 
	    <input type="file" name="file" class="input"> 
	    <input type="submit" name="submit_upload" value="上  传"> 
	    <input type="submit" name="submit_delete" value="删  除"> 
	    <iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe> 
	</form> 
	 
	<img id="picview" height="100" style="display:none;"> 
	//开源代码phprm.com 
	<script> 
	if("<?php echo $imgpath; "){ 
	    parent.document.getelementbyid("picview").src = "<?php echo $imgpath; "; 
	    parent.document.getelementbyid("picview").style.display = "block"; 
	} 
	</script> 

本文链接:http://www.phprm.com/tuxiang/fs5087.html

收藏随意^^请保留教程地址.

标签:php图片上传 php预览效果

相关文章

发表留言