Home >Backend Development >PHP Tutorial >PHP AjaxForm submits image upload and displays image

PHP AjaxForm submits image upload and displays image

墨辰丷
墨辰丷Original
2018-05-30 10:18:261497browse

This article mainly introduces PHP AjaxForm to submit image upload and display image source code in detail. It has certain reference value. Interested friends can refer to

PHP dofile.php file upload source code.

<?php
$file_upload = "upload/";
$file_allow_ext=&#39;gif|jpg|jpeg|png|gif|zip|rar|ppt|xls|pdf|pptx|xlsx|docx&#39;;
$file_allow_size = 5*1024*1024;
if($_POST[&#39;submit&#39;]=="上传"){
 if(is_uploaded_file($_FILES[&#39;file&#39;][&#39;tmp_name&#39;])){
  $file_name = $_FILES[&#39;file&#39;][&#39;name&#39;];
  $file_error = $_FILES[&#39;file&#39;][&#39;error&#39;];
  $file_type = $_FILES[&#39;file&#39;][&#39;type&#39;];
  $file_tmp_name = $_FILES[&#39;file&#39;][&#39;tmp_name&#39;]; 
  $file_size = $_FILES[&#39;file&#39;][&#39;size&#39;];
  $file_ext = substr($file_name, strrpos($file_name, &#39;.&#39;)+1);
  switch($file_error){
  case 0:
  $data[&#39;status&#39;] = 0;
  $data[&#39;msg&#39;] = "文件上传成功!";  
  break;

  case 1:
  $data[&#39;status&#39;] = 1;
  $data[&#39;msg&#39;] = "文件上传失败,文件大小".$file_size."超过限制,允许上传大小".sizeFormat($file_allow_size)."!";  
  break;

  case 3:
  $data[&#39;status&#39;] = 1;
  $data[&#39;msg&#39;] = "上传失败,文件只有部份上传!";  
  break;

  case 4:
  $data[&#39;status&#39;] = 1;
  $data[&#39;msg&#39;] = "上传失败,文件没有被上传!";  
  break;

  case 5:
  $data[&#39;status&#39;] = 1;
  $data[&#39;msg&#39;] = "文件上传失败,文件大小为0!";  
  break; 
  }
  if(stripos($file_allow_ext,$file_ext)===false){
  $data[&#39;status&#39;] = 1;
  $data[&#39;msg&#39;] = "该文件扩展名不允许上传";  
  }
  if($file_size>$file_allow_size){  
  $data[&#39;status&#39;] = 1;
  $data[&#39;msg&#39;] = "文件大小超过限制,只能上传".sizeFormat($file_allow_size)."的文件!"; 
  } 
  if($data[&#39;status&#39;]==1){
  $data[&#39;status&#39;] = 1;
  $data[&#39;msg&#39;] = $data[&#39;msg&#39;];
  exit(json_encode($data)); 
  }
  if($data[&#39;status&#39;]==0){
  if(file_exists($file_upload)){   
   $file_new_name = date("YmdHis").&#39;_&#39;.rand(10000,99999).&#39;.&#39;.$file_ext;
   $file_save_path = $file_upload.$file_new_name; 
   $data[&#39;status&#39;] = 0;  
   $data[&#39;url&#39;] = $file_save_path;
   move_uploaded_file($file_tmp_name,$file_save_path);
   exit(json_encode($data));    
  }else{
   exit(json_encode($data));
  }  

  }   
 }
}

function sizeFormat($size)
{
 $sizeStr=&#39;&#39;;
 if($size<1024)
 {
  return $size."bytes";
 }
 else if($size<(1024*1024))
 {
  $size=round($size/1024,1);
  return $size."KB";
 }
 else if($size<(1024*1024*1024))
 {
  $size=round($size/(1024*1024),1);
  return $size."MB";
 }
 else
 {
  $size=round($size/(1024*1024*1024),1);
  return $size."GB";
 } 
}



?>

The HTML is as follows

<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://files.cnblogs.com/files/china-li/jquery.form.js"></script>

<form action="dofile.php" method="post" enctype="multipart/form-data" id="upfileimage">
<input type="hidden" name="image[]" />
<label for="file">文件:</label><input type="file" name="file" id="file" />
<input type="submit" name="submit" value="上传" />
</form>

<script type="text/javascript">
$("#upfileimage").submit(function(){
 if($("input[type=file]").val()==""){
  alert("请选择要上传的文件");
  return false;
 }
})
$(function(){
 var options = {  
  type:"POST",
  dataType:"json",
  resetForm:true,
  success:function(o){
   if(o.status==1){
   alert(o.msg);
   }else{
   $("body").append("  <img src=&#39;"+o.url+"&#39; alt=&#39;&#39; width=&#39;100&#39; /><input type=&#39;hidden&#39; name=&#39;image[]&#39; value=&#39;"+o.url+"&#39; />");
   }
  },
  error:function(o){
   alert(o.message);
  }  
 }
 $("#upfileimage").ajaxForm(options).submit(function(){return false;});

})
</script>

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

How to implement a mortgage calculator in PHP

##phpThe file contains the directory configuration open_basedir usage and performance

##php Implement the method of calling ffmpeg to obtain video information

The above is the detailed content of PHP AjaxForm submits image upload and displays image. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn