Jump to content

Delete user uploaded files with php


ArshSingh

Recommended Posts

so the thing is ,, i have the following code to retrieve the files from user's folder and show them in myfiles.php  now the question is , how i can delete the file i want to delete with php code, i have already got the viewfile page but i want to delete the files from page myfiles.php without going to cpanel , so users can vieww and delete their files.

<?php
include("db-settings.php");
include("config.php");
$view = $_REQUEST['viewfile'];
$directory = "uploads/$loggedInUser->username$view/";
if (is_dir($directory)) {
				if ($directory_handle = opendir($directory)) {
								while (($file = readdir($directory_handle)) !== false) {
												$filet     = "uploads/$loggedInUser->username$view/$file";
												$thumbimg = "uploads/$loggedInUser->username$view/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=$view&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=$view&file=$file\">";
																				$showthis = 1;
																				break;
																case "mp3":
																				$filetype   = "sound";
																				$actionlink = "<a href=\"view.php?folder=$view&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=$view&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=$view&file=$file\">";
																				$showthis = 1;
																				break;
																case "folder":
																				$filetype   = "folder";
																				$actionfile = "<img src=\"include/img/filetype/folder.jpg\" height=\"130\" width=\"130\">";
																				$actionlink = "<a href=\"files.php?folder=$view$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=$view&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></li>";
															}
								}
								closedir($directory_handle);
				}
}
?>
Link to comment
https://forums.phpfreaks.com/topic/286443-delete-user-uploaded-files-with-php/
Share on other sites

You need to create a delete link for each file, passing in the file to be deleted in the link. The delete link will be similar to the link used for viewing the file. Example delete link could be

                        $deletelink = "<a href=\"delete.php?folder=$view&file=$file\">Delete</a>";

Now in delete.php you'd make sure a) the user owns that file (it should be in their own folder) and b) the file exists. Then you you can pass the file to the unlink function suggested by paddyfields to delete it.

You need to create a delete link for each file, passing in the file to be deleted in the link. The delete link will be similar to the link used for viewing the file. Example delete link could be

                        $deletelink = "<a href=\"delete.php?folder=$view&file=$file\">Delete</a>";

Now in delete.php you'd make sure a) the user owns that file (it should be in their own folder) and b) the file exists. Then you you can pass the file to the unlink function suggested by paddyfields to delete it.

i've done that thanks ,, but is there anyway delete teh file without leaving current page?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.