2014-02-23 53 views
0

我允許我的用戶從他們的賬戶和文件夾刪除他們自己的文件,但每當有人點擊刪除按鈕url將他帶到remove.php並離開myfiles.php和文件被刪除。刪除文件不離開頁面

我只是想刪除文件,而無需離開下面的頁面是,我已經簽出的代碼功能,但沒有更迭:

阿賈克斯

<script> 
function loadXMLDoc() 
{ 
var xmlhttp; 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
{ 
if (xmlhttp.readyState==4 && xmlhttp.status==200) 
{ 
    // use the xmlhttp.responseText however you need. 
} 
} 
xmlhttp.open("POST", "remove.php?file=$actfolder&file=$file, true); 
xmlhttp.send(); 
} 
</script> 

remove.php

<?php 
require_once("models/config.php"); 
if (!securePage($_SERVER['PHP_SELF'])){die();} 
require("models/db-settings.php"); 

if(isset($_GET['file'])){ 
$filename = "uploads/$loggedInUser->username/" . ltrim($_GET['file'], '/\\'); 
// make sure only deleting a file in files/ directory 
if (dirname(realpath($filename)) == realpath("uploads/$loggedInUser->username")) { 
    unlink($filename); 
} 
} 
?> 

myfiles.php

<?php 
include("db-settings.php"); 
include("config.php"); 
$actfolder = $_REQUEST['folder']; 
$directory = "uploads/$loggedInUser->username/$actfolder/"; 
if (is_dir($directory)) { 
      if ($directory_handle = opendir($directory)) { 
          while (($file =  readdir($directory_handle)) !== false) { 
               $filet  = "uploads/$loggedInUser->username$actfolder/$file"; 
               $thumbimg = "uploads/$loggedInUser->username$actfolder/thumbs/$file"; 
               $path_info = pathinfo($filet); 

                if (array_key_exists('extension', $path_info)) { 
                   $extension = $path_info['extension']; 
               } else { 
                   $extension = "folder"; 
                } 

              switch ($extension) { 
                  case "jpg": 
                  case "png": 
                  case "gif": 
                  case "bmp": 
                      $filetype = "image"; 
                      if (file_exists($thumbimg)) { } 
                      else { 
                       include "SmartImage.class.php"; 
                       $img = new SmartImage($filet); 
                       $img -> resize(130, 130, true); 
                       $img -> saveImage("$directory"."thumbs/$file", 85); 
                      } 
                      $actionfile = "<img src=\"$thumbimg\" height=\"130\" width=\"130\">"; 
                      $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; 
                      $showthis = 1; 
                      break; 
                  case "txt": 
                  case "doc": 
                  case "docx": 
                  case "odt": 
                  case "ods": 
                  case "odp": 
                  case "xls": 
                  case "xlsx": 
                  case "pdf": 
                      $filetype = "text"; 
                      $actionfile = "<img src=\"include/img/filetype/text.jpg\" height=\"130\" width=\"130\">"; 
                      $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; 
                      $showthis = 1; 
                      break; 
                  case "mp3": 
                      $filetype = "sound"; 
                      $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; 
                      $actionfile = "<img src=\"img/music.jpg\" height=\"130\" width=\"130\"> 
              <div class=\"fl-au-player\"> 
              <object type=\"application/x-shockwave-flash\" data=\"players/audio/player_mp3_maxi.swf\" width=\"25\" height=\"20\"> 
               <param name=\"movie\" value=\"players/audio/player_mp3_maxi.swf\" /> 
               <param name=\"bgcolor\" value=\"#ffffff\" /> 
               <param name=\"FlashVars\" value=\"mp3=$filet&amp;width=25&amp;showslider=0&amp;bgcolor1=444444&amp;bgcolor2=444444&amp;buttonovercolor=dddddd\" /> 
              </object> 
              </div> 
              "; 
                      $showthis = 1; 
                      break; 
                  case "ogg": 
                  case "wav": 
                      $filetype = "sound"; 
                      $actionfile = "<img src=\"include/img/filetype/sound.jpg\" height=\"130\" width=\"130\">"; 
                      $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; 
                      $showthis = 1; 
                      break; 
                  case "avi": 
                  case "mpeg": 
                  case "wmv": 
                  case "mp4": 
                  case "3gp": 
                  case "flv": 
                      $filetype = "video"; 
                      $actionfile = "<img src=\"include/img/filetype/video.jpg\" height=\"130\" width=\"130\">"; 
                      $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; 
                      $showthis = 1; 
                      break; 
                  case "folder": 
                      $filetype = "folder"; 
                      $actionfile = "<img src=\"include/img/filetype/folder.jpg\" height=\"130\" width=\"130\">"; 
                      $actionlink = "<a href=\"myfiles.php?folder=$actfolder$file/\">"; 
                      if ($file == "thumbs") { 
                       $showthis = 0; 
                      } 
                      else { 
                       $showthis = 1; 
                      } 
                      break; 
                  default: 
                      $filetype = "other"; 
                      $actionfile = "<img src=\"include/img/filetype/other.jpg\" height=\"130\" width=\"130\">"; 
                      $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; 

                      $showthis = 1; 
              } 

              if (strlen($file) <= 19) { 
                  $filestamp = "$file"; 
              } else { 
                  $filestamp = "..." . substr("$file", -16); 
              } 

              if ((!is_dir($file)) & ($file != ".") & ($file != "..")) 
                 if ($showthis){ 
                  echo "<li class=\"$filetype\">$actionlink$actionfile</a>$actionlink<p>" . $filestamp . "</p></a><a href='remove.php?file=$actfolder&file=$file' title='Delete file '$file' from the server'>Delete</a></li>"; 
                 } 
          } 
          closedir($directory_handle); 
      } 
} 
?> 

這個代碼是每個文件下: <a href='remove.php?file=$actfolder&file=$file' title='Delete file '$file' from the server'>Delete</a> 預先感謝

+2

爲此使用'AJAX'。 –

+0

我用它,但我不可戰勝,使其工作 – anmo

+0

因此,使用post方法的形式,代碼粘貼在同一頁面,而不是一個單獨的文件,並在刪除後使用'header('Location:'$ _ SERVER ['' ' –

回答

0

在的,你可以去remove.php回到以前的頁面,此代碼末尾:

header("Location:".$_SERVER["HTTP_REFERER"]); 
+0

'$ referer'會持有什麼樣的? –

+0

感謝您指出這一點。對不起,我沒有檢查就複製了代碼。我更新了我的代碼。也許你的解決方案更好,我不確定你有什麼不同。 –

+0

thnxxxxxxxxxxxx它工作;)你們都謝謝你們 – anmo