Blogger Information
Blog 42
fans 4
comment 0
visits 30535
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4.19 php jquery ajax 图片上传--24Day
小丑的博客
Original
704 people have browsed it
  1. html页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Document</title>
    	<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    	<script type="text/javascript">
    	$(document).ready(function(){
    
    		$('button').click(function(){
    
    			// alert($('#fileName').val())
    			
    
    			var fileName = $('#fileName').val();			
    			var fileJpg = $('#fileJpg')[0].files[0];
    
    
    			
    			var formData = new FormData();  
      
                formData.append('fileName',fileName);                           
                formData.append('fileJpg',fileJpg);  //
    	        
    	        // console.log($('#fileJpg')[0].files[0]);
                 
    			$.ajax({
    
    				type:'post',
    				url:'server1.php',
    				//fileElementId:'fileJpg',
    				// data:{
    				// 	fileName:$('#fileName').val()
    				// },
    				data:formData,
    				//如果传递的是FormData数据类型,那么下来的三个参数是必须的,否则会报错
    				cache:false,  //默认是true,但是一般不做缓存  
    	            processData:false, //用于对data参数进行序列化处理,这里必须false;如果是true,就会将FormData转换为String类型  
    	            contentType:false,  //
    	            dataType:"json", //声明成功使用json数据类型回调  
    				success:function(msg){
    					$('p').append('图片保存文件夹:'+msg.fileName+",图片名称:"+msg.fileJpg);
    				},error:function(data,status,e){
    					alert(e)
    				}
    
    
    
    			})
    
    
    
    		})
    
    	})
    	</script>
    </head>
    <body>
    	文件夹: <input type="text" name="fileName" id='fileName'><br>
    	选择文件:<input type="file" name="fileJpg" id="fileJpg"><br>
    	<button>提交</button>
    	<p></p>
    </body>
    </html>

    运行实例 »

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

  2. PHP

实例

<?php 

$fileName = $_POST['fileName'];
//print_r($fileName);

if(!file_exists($fileName))
	mkdir($fileName);

$type = ['image/jpg','image/jpeg', 'image/png'];

if(in_array($_FILES['fileJpg']['type'],$type)){
	if(file_exists($fileName.'/'.$_FILES['fileJpg']['name'])){  

    	die($_FILES['fileJpg']['name']."文件已存在");  //如果上传的文件已经存在 

	}else{

	    move_uploaded_file($_FILES['fileJpg']['tmp_name'], $fileName.'/'.$_FILES['fileJpg']['name']);  //保存在缓冲区的是临时文件名而不是文件名  
	}  

}else{

	die("格式错误,必须为图片格式");
}


$json_array = array('fileName'=>$fileName,'fileJpg'=>$_FILES['fileJpg']['name']); //转换成数组类型  
     //$json_array = array('name'=>$username,'age'=>$age ,'file1'=>$filesName[0] ); //转换成数组类型  
  
$json= json_encode($json_array);  //将数组转换成json对象  
echo   $json;  

?>

运行实例 »

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

运行界面

微信图片_20180423164633.png

Correction status:Uncorrected

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