jonoc33 Posted October 28, 2007 Share Posted October 28, 2007 I got a challenge for you PHP people. I've been having trouble with this for a while. Ages ago I made an upload script that lets you upload to files/upload/ and then once you upload it, a piece of PHP will show the files in that folder. In the admin section of my page i'm trying to find a way to delete the files off the server without having to dig into the FTP and do it manually. Are you able to do that with PHP? So basically I want it to say: Uploaded Files: -file.jpg [DELETE] The code that displays the files of the folder is this: $folder = "../files/upload/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "<br />"."-<a href=$folder$file>$file</a>"; } If above is completely impossible, plan B would be to have a box where you could type in the name of the file, hit submit and it would take you to a page where the script deletes it off. Any help would be great. Jono Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/ Share on other sites More sharing options...
rajivgonsalves Posted October 28, 2007 Share Posted October 28, 2007 you can check out the unlink function http://www.php.net/manual/en/function.unlink.php Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-379673 Share on other sites More sharing options...
jonoc33 Posted October 28, 2007 Author Share Posted October 28, 2007 I thought up something.. After it echos the file name it echos this: echo " -<a href=delete.php?file=$file>Delete</a>"; $file has already been defined as the file name, so that works. So now when it goes to delete.php, i'm not sure what to do. Unlink didn't seem to help me much Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-379675 Share on other sites More sharing options...
jonoc33 Posted October 28, 2007 Author Share Posted October 28, 2007 Ok (sorry for double posting), i'm using this script for delete.php <?php $file = ''; // set up basic connection $conn_id = ftp_connect($ftp_server); $ftp_server = "localhost"; $ftp_user_name = "jonoc33"; $ftp_user_pass = "******"; // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); // try to delete $file if (ftp_delete($conn_id, $file)) { echo "$file deleted successful\n"; } else { echo "could not delete $file\n"; } // close the connection ftp_close($conn_id); ?> In $file, how do I get the name of the file from the previous page? Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-379678 Share on other sites More sharing options...
rajivgonsalves Posted October 28, 2007 Share Posted October 28, 2007 $file = $_GET['file']; you will have to put some sort of authentication otherwise anyone will be able to access the URL and delete files from the server Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-379696 Share on other sites More sharing options...
jonoc33 Posted October 28, 2007 Author Share Posted October 28, 2007 Doesn't seem to work. It probably doesn't because there is no form or text box on the previous page called file. I've tried everything but nothing works. EDIT: Works now, but it still does not delete it. It says "Could not delete x.jpg" at least. Still, it won't delete. Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-379718 Share on other sites More sharing options...
GingerRobot Posted October 28, 2007 Share Posted October 28, 2007 Well, the second parameter of ftp_delete is the path to the file. You appear to be just using the file name? Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-379725 Share on other sites More sharing options...
jonoc33 Posted October 29, 2007 Author Share Posted October 29, 2007 Well, the second parameter of ftp_delete is the path to the file. You appear to be just using the file name? Thats probably why.. Let me take a look at it EDIT: $file = '../files/upload/'.$_GET['file']; Echos: Could not delete ../files/upload/x.jpg *Sigh* Why doesnt PHP just do what I want it to do!!! lol Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-380150 Share on other sites More sharing options...
jonoc33 Posted October 29, 2007 Author Share Posted October 29, 2007 Works now. I did the actual root of the website and it works fine now. But it says this: mainwebsite_html/squads/alpha/files/upload/x.jpg deleted successfully. How would I make it so it doesn't show mainwebsite_html/squads/alpha/files/upload/ ? Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-380152 Share on other sites More sharing options...
~n[EO]n~ Posted October 29, 2007 Share Posted October 29, 2007 You have echoed the file path here <?php if (ftp_delete($conn_id, $file)) { echo "$file deleted successful\n"; ?> If you remove that $file in echo, path won't be shown. Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-380155 Share on other sites More sharing options...
jonoc33 Posted October 29, 2007 Author Share Posted October 29, 2007 True, I suppose I could just do that. But would there be any code to cancel that folder path out and leave just the file name? Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-380157 Share on other sites More sharing options...
~n[EO]n~ Posted October 29, 2007 Share Posted October 29, 2007 You can save name of file in different variable and echo that like you did for path <?php $file = '../files/upload/'.$_GET['file']; $filename = $_GET['file']; ?> and you can echo the filename, just try it I am not too sure. OR you can try explode() and preg_split() Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-380160 Share on other sites More sharing options...
jonoc33 Posted October 29, 2007 Author Share Posted October 29, 2007 n~ link=topic=165393.msg727653#msg727653 date=1193637234] <?php $file = '../files/upload/'.$_GET['file']; $filename = $_GET['file']; ?> Worked.. Don't know why I didnt think of that. Thanks for all your help guys Quote Link to comment https://forums.phpfreaks.com/topic/75069-solved-deleting-a-file-from-server-through-a-file-list/#findComment-380162 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.