7 Replies Latest reply: May 23, 2012 1:07 PM by esdebon RSS

    can i delete ALL files in directory ?

    Venkom Community Member

      i create a upload test using FileReference & PHP.

      In this scenario user upload files to http server.

       

      Now how can i delete all files in /files/uploads folder in AS3

       

      AS3 Code:

       

      req = new URLRequest();
      req.url = ( stage.loaderInfo.parameters.f )? stage.loaderInfo.parameters.f : "http://www.website.com/test/upload.php";
      uploadFile = new FileReference();
      
      select_btn.addEventListener( MouseEvent.CLICK, browse );
      uploadFile.addEventListener( Event.COMPLETE, complete_func );
      uploadFile.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, show_message );
      
      function browse( e:MouseEvent )
      {
                filefilters = [new FileFilter('Images',"*.jpg;*.png;*.gif")];
                uploadFile.browse( filefilters );
      }
      
      function complete_func( e:Event )
      {
                trace( 'complete !' );
      }
      
      function show_message(e:DataEvent)
      {
      if (e.data == 'ok')
                {
                label_txt.text = 'The file has been uploaded.';
                }
      else if ( e.data == 'error')
                {
                label_txt.text = 'The file could not be uploaded.';
                }
      }
      
      }
      

       

      PHP Code:

       

      <?php
      $uploads_dir = './files/uploads';
      
      if( $_FILES['Filedata']['error'] == 0 ){
                if( move_uploaded_file( $_FILES['Filedata']['tmp_name'], $uploads_dir.$_FILES['Filedata']['name'] ) ){
                          echo 'ok';
                          exit();
                }
      }
      echo 'error';
      exit();
      ?>
      
        • 1. Re: can i delete ALL files in directory ?
          esdebon Community Member

          You need call another PHP file

           

           

           

          <?php

           

             $directory = './files/uploads';

            

             getDirectoryList ($directory);  

           

             //This function find all the files in the directory

             function getDirectoryList ($directory)

             {

           

               // create an array to hold directory list

               $results = array();

           

               // create a handler for the directory

               $handler = opendir($directory);

           

               // open directory and walk through the filenames

               while ($file = readdir($handler)) {

           

                 // if file isn't this directory or its parent, add it to the results

                 if ($file != "." && $file != "..") {

           

              //$results[] = $file;

           

               // Delete Files

              $filename = $file;
           
          unlink($filename
          ); //this delete a file

           

           

                }

           

               }

           

               // tidy up: close the handler

               closedir($handler);

           

             }

           

          ?>

          • 2. Re: can i delete ALL files in directory ?
            Venkom Community Member

            how to call this script from flash?

            • 3. Re: can i delete ALL files in directory ?
              esdebon Community Member

              var request:URLRequest = new URLRequest ("deletFiles.php");

               

              var loader:URLLoader = new URLLoader (request);

              //loader.addEventListener(Event.COMPLETE, onComplete);

              loader.load(request);

              • 4. Re: can i delete ALL files in directory ?
                Venkom Community Member

                i tried this but not working....

                 

                my code:

                 

                var request:URLRequest = new URLRequest ("http://www.site.com/test/deletfiles.php");

                 

                var loader:URLLoader = new URLLoader (request);

                loader.addEventListener(Event.COMPLETE, onComplete);

                loader.load(request);

                 

                function onComplete(e:Event):void

                {

                          trace("Complete");

                }

                 

                PHP

                 

                <?php

                 

                   $directory = './files/uploads';

                   getDirectoryList ($directory); 

                 

                   function getDirectoryList ($directory)

                   {

                 

                    $filename = $file;

                    unlink($filename); //this delete a file

                 

                    closedir($handler);

                 

                   }

                 

                ?>


                • 5. Re: can i delete ALL files in directory ?
                  esdebon Community Member

                  Change:

                   

                   

                  <?php

                   

                     $directory = './files/uploads';

                     getDirectoryList ($directory); 

                   

                     function getDirectoryList ($directory)

                     {

                   

                      $filename = $file;

                      unlink($filename); //this delete a file

                   

                      closedir($handler);

                   

                     }

                   

                  ?>

                   

                   

                   

                   

                  for this:

                   

                   

                   

                  <?php

                   

                     $directory = './files/uploads';

                     getDirectoryList ($directory);  

                   

                     function getDirectoryList ($directory)

                     {

                   

                       $results = array();

                       $handler = opendir($directory);

                   

                       while ($file = readdir($handler)) {

                   

                         if ($file != "." && $file != "..") {

                   

                                    $filename = $file;
                        
                  unlink($filename);

                        }

                       }

                       closedir($handler);

                   

                     }

                  ?>

                  • 6. Re: can i delete ALL files in directory ?
                    Venkom Community Member

                    i'm facing same problem. i think problem is in flash loader.

                    if i enter wrong path flash showing : trace complete.

                    • 7. Re: can i delete ALL files in directory ?
                      esdebon Community Member

                      if Flash trace "complete" this means that the loader is working.

                       

                      Copy some files to the folder and try testing the PHP script

                      in a navigator go to:

                      http://www.site.com/test/deletfiles.php

                       

                      (where site is your real domain)