Advertisement
Guest User

Untitled

a guest
Sep 17th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.46 KB | None | 0 0
  1. <?php
  2.  
  3.    if($_SERVER['REQUEST_METHOD']=='POST'){
  4.        echo "hello 1";
  5.    }
  6.  
  7.    if(!empty($_FILES['files]'])){
  8.        echo "hello 2";
  9.    }
  10.  
  11.    echo "hello 3";
  12.  
  13.    if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_FILES ) ){
  14.        ob_clean();
  15.  
  16.        /*
  17.            process the uploaded files
  18.            --------------------------
  19.  
  20.            In the actual LIVE version you will want to check that
  21.            each file is legitimate and of the correct type before
  22.            saving to it's final location & possibly logging the
  23.            upload in the database.
  24.            check if there were errors
  25.            check filesize
  26.            check filetype
  27.            check is_uploaded_file
  28.            check if already exists
  29.            etc etc
  30.            For demonstration of the upload process this script ONLY
  31.            echoes back the details of the files uploaded.
  32.            YOU will need to do the image processing here....
  33.        */
  34.        /* example */
  35.        $output=array();
  36.        $files=(object)$_FILES[ 'files' ];
  37.        foreach( $files->name as $i => $void ){
  38.             $name = $files->name[$i];
  39.             $size = $files->size[$i];
  40.             $type = $files->type[$i];
  41.             $tmp  = $files->tmp_name[$i];
  42.             $error= $files->error[$i];
  43.  
  44.             $output[]=array('name'=>$name,'size'=>$size,'type'=>$type);
  45.         }
  46.         exit( json_encode( $output ) );
  47.     }
  48. ?>
  49. <!doctype html>
  50. <html>
  51.     <head>
  52.         <meta charset='utf-8' />
  53.         <title>Browse multiple locations</title>
  54.         <script>
  55.             (function(){
  56.                 function ajax(url,payload,callback){
  57.                     var xhr=new XMLHttpRequest();
  58.                     xhr.onreadystatechange=function(){
  59.                         if( this.readyState==4 && this.status==200 )callback.call( this, this.response );
  60.                     };
  61.                     xhr.open( 'POST', url, true );
  62.                     xhr.send( payload );
  63.                 }
  64.  
  65.  
  66.                 document.addEventListener('DOMContentLoaded',function(){
  67.  
  68.                     let fd=new FormData();
  69.  
  70.                     const callback=function(r){
  71.                         console.info( r )
  72.                         let json=JSON.parse( r );
  73.                         fd=new FormData();
  74.                         document.getElementById('count').innerHTML=Object.keys( json ).length + ' files uploaded';
  75.                     };
  76.  
  77.                     let oFile=document.querySelector('input[type="file"]');
  78.                     let oBttn=document.querySelector('input[type="button"]');
  79.  
  80.                     oFile.addEventListener( 'change', function(e){
  81.                         for( var i=0; i < this.files.length; i++ ) fd.append( 'files[]', this.files[ i ], this.files[ i ].name );
  82.                        document.getElementById('count').innerHTML=fd.getAll('files[]').length+' files in array';
  83.                    },false );
  84.  
  85.  
  86.  
  87.  
  88.                    oBttn.addEventListener( 'click', function(e){
  89.                        if( fd.getAll('files[]').length > 0 ) ajax.call( this, location.href, fd, callback );
  90.                     },false );
  91.  
  92.                 }, false );
  93.             })();
  94.         </script>
  95.     </head>
  96.     <body>
  97.         <form>
  98.             <div id='count'></div>
  99.             <input type='file' name='files' multiple />
  100.             <input type='button' value='Upload Files' />
  101.         </form>
  102.     </body>
  103. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement