2013-01-20 70 views
3
function remove_directory($directory) { 
     if (is_dir($directory) === true) { 
      $contents = scandir($directory); 
      unset($contents[0], $contents[1]); 

      foreach($contents as $object) { 
       $current_object = $directory.'/'.$object; 
       if (filetype($current_object) === 'dir') { 
        remove_directory($current_object); 
        } else { 
        unlink($current_object);  
        } 
       } 
       rmdir($directory); 
      } 
     } 

    function delete_row($filecode) { 
     $filecode = mysql_real_escape_string($filecode); 

     mysql_query("DELETE FROM `files` WHERE `code` = '$filecode'"); 

     } 
      $query = mysql_query("SELECT `id`, `username`, `title`, LEFT(`description`, 90) as `description`, `code`, `type`, `size`, `date` FROM `files` WHERE `username` = '$userfile' ORDER BY id DESC LIMIT $start, $per_page"); 

     while ($query_row = mysql_fetch_assoc($query)) { 
      $fileuser = $query_row['username']; 
      $filetitle = $query_row['title']; 
      $filecode = $query_row['code']; 
      $filedesc = $query_row['description']; 
      $filesize = $query_row['size']; 
      $filedate = $query_row['date']; 
      $filetype = $query_row['type']; 

      if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['dir'])) { 
      $dir = basename($_POST['dir']); 
      if ($dir[0] != '.') 
      remove_directory("files/$dir"); //these function work good and delete only one folder when I click Delete button 
      delete_row($filecode); //these function delete me all row in database in first page when I click in second page delete button too what the .... I am so angry i cant believe what is happend it's gone my database 

     } 

     if (empty($filedesc) === false) { 
     echo '<div id="linkstyle"><strong><a href="http://localhost/edu/1111111111111/userdownload.php?code='. $filecode . ' ">' , $filetitle , '</a></strong></div> <div id="displayfiledesc">' , $filedesc , '...</div><div id="displayfiledate"> &#149; ' , formatBytes($filesize) , ' &#149; ' , $filedate , ' &#149; ' , $filetype , '</div>';?> 

     <form action="" method="post"> 
      <input type="hidden" name="dir" value="<?= $filecode?>"> 
      <input type="submit" name="delete" id="bdelete" value="Delete"><br><br> 
     </form> 

     <?php 
     } else { 
     echo '<div id="linkstyle"> <strong><a href="http://localhost/edu/1111111111111/userdownload.php?code='. $filecode . ' ">' , $filetitle , '</a></strong></div> <div id="displayfiledate"> &#149; ' , formatBytes($filesize) , ' &#149; ' , $filedate , ' &#149; ' , $filetype , '</div>';?> 

     <form action="" method="post"> 
      <input type="hidden" name="dir" value="<?= $filecode?>"> 
      <input type="submit" name="delete" id="bdelete" value="Delete"><br><br> 
     </form> 

我的問題太大了。當我點擊刪除按鈕,我希望它是從我的MySQL數據庫刪除一個文件夾和一行,但沒有它刪除一個文件夾和mysql數據庫中的所有行在第一頁,我可以看到刪除按鈕。我能理解的問題在哪裏?有人能幫助我嗎?php刪除文件夾,從mysql數據庫刪除一行提交刪除按鈕

回答

1

您在循環中調用delete_row(),該循環遍歷SELECT ... FROM files WHERE username = '$userfile'的結果集,該結果集僅受LIMIT子句的限制。

雖然remove_directory()也在該循環中重複調用,但始終使用相同的參數調用它:'files/'.basename($_POST['dir'])

+0

謝謝大家。我認爲8小時,我繼續想,但它是如此簡單... – user1929805

+0

Offf,但現在它是從MySQL數據庫刪除我一行它是非常好的,但也刪除我也一樣點擊兩個文件夾 - 點擊按鈕文件夾,然後文件夾...現在有什麼問題 – user1929805

+1

@ user1929805:我不知道。我看不到您的修改代碼。 – eggyal