Php 删除文件而不离开页面

Php 删除文件而不离开页面,php,unlink,Php,Unlink,我允许我的用户从他们的帐户和文件夹中删除他们自己的文件,但每当有人单击删除按钮时,url就会将他带到remove.php并离开myfiles.php,文件就会被删除 我只想在不离开页面的情况下删除文件以下是我已签出但未成功的代码函数: Ajax <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,

我允许我的用户从他们的帐户和文件夹中删除他们自己的文件,但每当有人单击删除按钮时,url就会将他带到
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>

myfiles.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);
}
}
?>
<?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);
            }
}
?>

此代码位于每个文件下:

提前感谢

在remove.php的末尾,您可以使用以下代码返回上一页:

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

为此,请使用
AJAX
。我使用了它,但我无法让它工作。请使用带有post方法的表单,将代码粘贴到同一页面上,而不是单独的文件,删除后请使用
标题('Location:'$\u SERVER['REQUEST\u URI'])
@u_mulder Ajax不是必需的,请理解这个问题:)我没有使用form方法删除文件,我正在使用
链接中的veriables。看一看问题的结尾,
$referer
包含哪些内容?感谢您指出这一点。对不起,我没有检查就复制了代码。我更新了我的代码。也许你的解决方案更好,我不确定你的区别是什么。Thnxxxxxxxxxx它可以工作;)谢谢你们两个