PHP AjaxForm提交图片上传并显示图片源码_php实例

WBOY
Lepaskan: 2023-03-03 07:30:01
asal
1493 orang telah melayarinya

本文实例为大家分享了PHP AjaxForm提交图片上传并显示图片的具体代码,供大家参考,具体内容如下

PHP dofile.php 文件上传源码

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

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

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

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

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



&#63;>


Salin selepas log masuk

HTML如下

<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='"+o.url+"' alt=''    style="max-width:90%" /><input type='hidden' name='image[]' value='"+o.url+"' />");
   }
  },
  error:function(o){
   alert(o.message);
  }  
 }
 $("#upfileimage").ajaxForm(options).submit(function(){return false;});

})
</script>

Salin selepas log masuk

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!