ArshSingh Posted February 23, 2014 Share Posted February 23, 2014 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); } } ?> Quote Link to comment Share on other sites More sharing options...
paddy_fields Posted February 23, 2014 Share Posted February 23, 2014 (edited) use unlink() to delete a file. $filename = "myfile.txt"; unlink($filename); Edited February 23, 2014 by paddyfields Quote Link to comment Share on other sites More sharing options...
ArshSingh Posted February 23, 2014 Author Share Posted February 23, 2014 use unlink() to delete a file. $filename = "myfile.txt"; unlink($filename); but can u please tell me how to include it in that code under every file? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted February 23, 2014 Share Posted February 23, 2014 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. Quote Link to comment Share on other sites More sharing options...
ArshSingh Posted February 23, 2014 Author Share Posted February 23, 2014 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? Quote Link to comment Share on other sites More sharing options...
paddy_fields Posted February 23, 2014 Share Posted February 23, 2014 You could use AJAX to deal with the request as it allows you to remain on the same page. I'd recommend using jQuery to make the AJAX request too, it makes it a lot more straight forward to code. Quote Link to comment Share on other sites More sharing options...
ArshSingh Posted February 23, 2014 Author Share Posted February 23, 2014 You could use AJAX to deal with the request as it allows you to remain on the same page. I'd recommend using jQuery to make the AJAX request too, it makes it a lot more straight forward to code. any link to make it easy? Quote Link to comment Share on other sites More sharing options...
paddy_fields Posted February 23, 2014 Share Posted February 23, 2014 There are lots of tutorials online to choose from... http://www.google.co.uk/search?q=ajax+jquery+tutorial Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.