I'm allowing my users to delete their own files from their account and folder but whenever someone clicks on the delete button url takes him to the remove.php
and leaves myfiles.php
and file is is removed .
I just want to delete files without leaving the page following are the codes functions which I've checked out but no succes :
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&width=25&showslider=0&bgcolor1=444444&bgcolor2=444444&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);
}
}
?>
this code is under every file :
<a href='remove.php?file=$actfolder&file=$file' title='Delete file '$file' from the server'>Delete</a>
Thanks in advance
At the end of remove.php you can go back to the previous page with this code:
header("Location:".$_SERVER["HTTP_REFERER"]);