[Php-Ajax] - Cancella Files

Only PHP & MySQL.

Moderator: DaD

[Php-Ajax] - Cancella Files

Postby k8 on Mon Dec 24, 2007 4:23 pm

Questo piccolo script cancella files in modo dinamico con l'uso di ajax e php :)
Mostra i files in una directory e da la possibilità di cancellarli.. tutto qua

File files.php
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
      <title>Delete Files :: Home</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">   
      <script type="text/javascript" src="js.js"></script>
</head>
<body>

<?php
   $dir = ".";
   $handle = opendir($dir);
    while(($file = readdir($handle)) !== FALSE)
    {   
       $percorso = "$dir/$file";
       if(is_file($file)){
         echo "<a href=\"$percorso\">$file</a>  <input type=\"button\" value=\"Cancella\" OnClick=\"Cancella('".$percorso."');\"><br>\n";
      }
   }
?>   
</body>
</html>

File delete.php
Code: Select all
<?php
   if(!empty($_POST["file"])){
      if(@unlink($_POST["file"])){
         echo 'File Cancellato';
      } else {
         echo 'Non hai i permessi per cancellare il file.';
      }
   }
?>


File js.js
Code: Select all
function Cancella(percorso){
   ajax = assegnaXMLHttpRequest();
    if(ajax){ 
          try {
         ajax.open("POST", "delete.php", true);   
         ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
         ajax.send("file="+percorso);
         ajax.onreadystatechange = function(){

            if (ajax.readyState == 4) {
               if (ajax.status == 200) {

                  if(ajax.responseText == "File Cancellato"){
                     alert(ajax.responseText);
                     location.reload();
                  } else {
                     alert(ajax.responseText);
                  }
               }
            }
         }
      } catch(e){
          alert("Errore: "+e);
       }
   }                          
}



function assegnaXMLHttpRequest() {
   var
      XHR = null,
      browserUtente = navigator.userAgent.toUpperCase();
   if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
      XHR = new XMLHttpRequest();
   else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
      if(browserUtente.indexOf("MSIE 5") < 0)
         XHR = new ActiveXObject("Msxml2.XMLHTTP");
      else
         XHR = new ActiveXObject("Microsoft.XMLHTTP");
   }
   return XHR;
};

Ringrazio Eagle per la funzioneassegnaXMLHttpRequest()

:)
Big-Bug cerca collaboratori.
Scrivete senza abbreviazioni e senza k, grazie.
k8
Admin
 
Posts: 2127
Joined: Tue Jun 06, 2006 4:00 pm

[Php-Ajax] - Cancella Files

Sponsor

Sponsor


Postby DaD on Tue Dec 25, 2007 8:46 pm

Iniziato anche te con ajax eh?
Cmq buon inizio brau :D
Image
Le tre grandi virtù di un programmatore: pigrizia, impazienza e arroganza. (Larry Wall)
Get Slack! | User Linux's ID | Blog
User avatar
DaD
Php Guru
 
Posts: 322
Joined: Sat Nov 25, 2006 5:09 pm

Postby k8 on Tue Dec 25, 2007 8:59 pm

dad wrote:Iniziato anche te con ajax eh?
Cmq buon inizio brau :D

Grazie :D
Te hai fatto qualcosa di fico^?! ;)
Big-Bug cerca collaboratori.
Scrivete senza abbreviazioni e senza k, grazie.
k8
Admin
 
Posts: 2127
Joined: Tue Jun 06, 2006 4:00 pm

Postby DaD on Tue Dec 25, 2007 9:17 pm

Stavo mettendo su un editor per file *.odt tipo come quello di gugol, ma alla fine mi sono scocciato xD
Image
Le tre grandi virtù di un programmatore: pigrizia, impazienza e arroganza. (Larry Wall)
Get Slack! | User Linux's ID | Blog
User avatar
DaD
Php Guru
 
Posts: 322
Joined: Sat Nov 25, 2006 5:09 pm

Postby k8 on Wed Dec 26, 2007 1:19 am

dad wrote:Stavo mettendo su un editor per file *.odt tipo come quello di gugol, ma alla fine mi sono scocciato xD

fico :o
Big-Bug cerca collaboratori.
Scrivete senza abbreviazioni e senza k, grazie.
k8
Admin
 
Posts: 2127
Joined: Tue Jun 06, 2006 4:00 pm


Return to PHP & MySQL

Who is online

Users browsing this forum: No registered users and 1 guest

cron