Home >Backend Development >PHP Tutorial >PHP+ajax image file asynchronous upload sample code

PHP+ajax image file asynchronous upload sample code

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 08:53:071378browse
  1. jQuery.extend({

  2. createUploadIframe: function(id, uri)
  3. {
  4. //create frame
  5. var frameId = 'jUploadFrame' + id;
  6. var iframeHtml = '

过程: (1 )前端文件的代码: test.php

复制代码

相应的HTML为:

复制代码

这样客户端就完成了。

(2) 在服务器端的doajaxfileupload.php 此处为了简便的检测是否真正的传值过来了,你可以将它存起来了。

  1. $file_infor = var_export($_FILES,true);
  2. file_put_contents("d:file_infor.php".$file_infor);
复制代码

这样打来刚生成的file_infor.php文件时,又看到了熟悉的信息了:

  1. array(
  2. 'name'=>'lamp.jpg',
  3. 'type'=>'image/pjpeg',
  4. 'tmp_name'=>'c:windowstempphpFA.tmp',
  5. 'error'=>0,
  6. 'size'=>3127
  7. )
复制代码

Of course, the real processing is similar to this:

  1. $upFilePath = "d://";
  2. $ok=@move_uploaded_file($_FILES['img']['tmp_name'],$upFilePath);
  3. if($ok = == FALSE){
  4. echo json_encode('file_infor'=>'Upload failed');
  5. }else{
  6. echo json_encode('file_infor'=>'Upload successful');
  7. }
  8. ?>
Copy the code

Method 2, use the iframe framework to upload images html code:

  • Copy the code

    index.js file:

    1. $(function(){
    2. $("#upload_file").change(function(){
    3. $("#uploadFrom").submit();
    4. });
    5. });
    6. function stopSend(str){
    7. var im="PHP+ajax image file asynchronous upload sample code";
    8. $("#msg").append(im);
    9. }
    Copy code

    upload.php file:

    1. $file=$_FILES['upfile'];
    2. $name=rand(0,500000).dechex(rand(0,10000)).".jpg";
    3. move_uploaded_file ($file['tmp_name'],"upload/images/".$name);
    4. //Call the js function of the iframe parent window
    5. echo "<script>parent.stopSend('$name')</script>> ;";
    6. ?>
    Copy code

    Method three, original ajax file upload

    1. Html5 Ajax upload file
  • > ;
  • 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