Home  >  Article  >  Backend Development  >  Summary of several methods of asynchronously uploading images in PHP_PHP Tutorial

Summary of several methods of asynchronously uploading images in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 11:10:501099browse

There are two commonly used methods to upload images asynchronously. One is to use iframe, and the other is to use ajax to achieve it. Generally, third-party plug-ins are used.

Upload image form submission target into a hidden iframe,

The code is as follows Copy code
 代码如下 复制代码

form action="upload.php" id="form1" name="form1" enctype="multipart/form-data" method="post" target="uploadIframe">


form action="upload.php" id="form1" name="form1" enctype="multipart/form- data" method="post" target="uploadIframe">


Then the background processes the upload image logic and returns to the front desk. Use ajax to modify the DOM object of the current page to achieve the friendly function of uploading images without refreshing.

Example
 代码如下 复制代码

a.html

 


       
       
       
 

The code is as follows
Copy code

a.html

 代码如下 复制代码
if ($_FILES["test_file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["test_file"]["error"] . "
";
  }
else
  {
//这里的判断图片属性的方法就不写了。自己扩展一下。
  $filetype=strrchr($_FILES["test_file"]["name"],".");
  $filetype=substr($filetype,1,strlen($filetype));
  $filename="img/".time("YmdHis").".".$filetype;
  move_uploaded_file($_FILES["test_file"]["tmp_name"],$filename);
  echo '';
  $return="parent.document.getElementByIdx_x('mpic".$pageset_id."').innerHTML='".$dataimgpath."'";
  echo "";
  echo "<script>{$return}</script>";
  }
?>

                                                            " ="test_file" type="file" id="test_file" size="48">
                                                                                           ;
PHP code:

其实jquery ajax图片异步上传

HTML:

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


  图片异步上传




 


 

  
 

 
 

 

 



 

index.js

$(function(){
 $("#upload_file").change(function(){
   $("#uploadFrom").submit();
 });
});


function stopSend(str){
 var im="";
 $("#msg").append(im);

}

 

upload.php

 $file=$_FILES['upfile'];
 $name=rand(0,500000).dechex(rand(0,10000)).".jpg";
 move_uploaded_file($file['tmp_name'],"upload/images/".$name);

//调用iframe父窗口的js 函数

 echo "<script>parent.stopSend('$name')</script>";
?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444678.htmlTechArticle要实现异步上传图片方法有常用的有二种,一种是利用iframe实现,另一种是借助于ajax来实现一般用第三方插件了。 上传图片form提交target到...
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