Blogger Information
Blog 48
fans 3
comment 1
visits 37475
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
图片上传功能制作——2018年4月19日
JackBlog
Original
558 people have browsed it



GIF.gif

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>图片上传测试</title>
</head>
<body>
	<form id="picupload" enctype="multipart/form-data">
			<fieldset>
				<legend>上传测试</legend>
				<label for='file'></label><input type="file" name="file" id='file'>
				<p><button type='submit' name='submit' id='submit'>上传</button></p>
				
				
			</fieldset>
		

	</form>
	
</body>
</html>

<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script>
<script type="text/javascript">
  
$('#submit').click(function(){
	var pic = new FormData();
    pic.append("file", $("#file").get(0).files[0]);

      $.ajax({
        url:'upload.php',
        type:"post",
        processData: false,
        contentType: false,
        data:pic,
        dataType:'json',
        success:function(res){  
        	
        	$('span').remove()
        	// $('p').after('<span>').next().html(res).css('color','green')
        	switch(res.status){
        		case 0:
        		$('p').after('<span>').next().html(res.msg).css('color','green')
        		break
        		case 1:
        		$('p').after('<span>').next().html(res.msg).css('color','red')
        		break
        		case 2:
        		$('p').after('<span>').next().html(res.msg).css('color','red')
        		break
        		case 3:
        		$('p').after('<span>').next().html(res.msg).css('color','red')
        		break
        	}
		}
      })
return false
})

</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例



实例

<?php


$randname =date('Hms');
$randname .= rand(0,999)*rand(0,999).'.jpg';
$allow = ['image/jpg','image/jpeg','image/png'];

$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];

if ($_SERVER['REQUEST_METHOD']=='POST') {
	if (isset($_FILES['file'])) {
		if(in_array($type,$allow)){
			if ($size < (1024*50)) {
				move_uploaded_file($tmp_name,'upload/'.$randname);

				exit(json_encode(['status'=>0,'msg'=>'图片上传成功']));


				
			}else{
				exit(json_encode(['status'=>1,'msg'=>'文件尺寸不得大于50KB!']));
			}
		}else{
			exit(json_encode(['status'=>2,'msg'=>'不支持此类型文件上传!']));
		}
	}else{
		exit(json_encode(['status'=>3,'msg'=>'文件没有选择!']));
	}
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post