Advertisement
Guest User

Untitled

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