删除文件而不离开页面

时间:2014-02-23 17:26:12

标签: php unlink

我允许我的用户从他们的帐户和文件夹中删除自己的文件,但每当有人点击删除按钮时,网址会将他带到remove.php并离开myfiles.php并删除文件。

我只是想在不离开页面的情况下删除文件是我检查过但没有成功的代码功能:

的Ajax

 <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> 提前致谢

1 个答案:

答案 0 :(得分:0)

在remove.php结束时,您可以使用以下代码返回上一页:

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