andre1990 Posted September 10, 2011 Share Posted September 10, 2011 Hey, I've got an upload site where you can upload files and you get a direct link. I was wondering if it would be possible to offer a delete link too, where if you went to the link it would delete your file? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/ Share on other sites More sharing options...
JKG Posted September 10, 2011 Share Posted September 10, 2011 yeah, just either create a file called delete.php then add the query string with the id or filename or something, then use $_GET to get the unique. or, in the same file, use an isset() to determine if delete is set in the url, eg: files.php?delete=1&id=19 then, in that if statement, put your deletion code. to delete you can use: unlink('/root/file/name/and/path/here.jpg'); <?php if(isset($_GET['delete'])) { unlink('/home/user/www/files/' . $_GET['file_name']); } Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267655 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 Thank you for the response. Could you just go through that in a bit more detail? Im still very bad & learning php! Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267656 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 JKG, would that code be in delete.php? Then to produce a link which could be selectable... <?php echo.... ?> Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267658 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 <?php ob_start(); session_start(); $extensions = array("jpg", "png","jpeg", "gif", "zip", "rar", "swf", "tiff", "bmp", "txt", "fla", "7z", "tar", "gz", "iso", "dmg", "mp3", "wav", "m4a", "aac", "doc", "docx", "xls", "rtf", "ppt", "bsd", "exe", "psd", "c4d", "pdf", "dwg", "max", "ipa", "vtf", "iam", "ipt", "flv", "scr"); $maxsize = 104288000; $server = "http://www.andre1990.com"; $name = $_FILES['file']['name']; $temp = $_FILES['file']['tmp_name']; $size = $_FILES['file']['size']; $random = md5(uniqid(rand(), true)); $random = substr($random, 0, 20); if (!$name || !$temp || !$size) { header("Location: index.php?feedback=Please select a file."); exit(); } foreach ($_FILES as $file) { if ($file['tmp_name'] != null) { $thisext1=explode(".", strtolower($file['name'])); $thisext=$thisext1[count($thisext1)-1]; if (!in_array($thisext, $extensions)) { header(sprintf("Location: index.php?feedback=The file extension \"%s\" is not allowed.", $thisext)); exit(); } } } if ($size > $maxsize) { header("Location: index.php?feedback=The file size is too large."); exit(); } $destination = "Uploads/".$random; mkdir($destination); move_uploaded_file($temp, $destination."/".$name); $final = $server."/".$destination."/".$name; ?> <?php ob_start(); ?> <!DOCTYPE html> <html> <head> <title>File Uploaded!</title> <link rel="stylesheet" href="style.css" type="text/css"> <link REL="SHORTCUT ICON" HREF="images/favicon.ico"> </head> <body> <div id="topbar"> <div class="content"> <div class="logo"><img src="images/logo.png" height="90"/></div> </div> </div> <div id="navbar"> <ul> <li><a href="http://www.andre1990.com" id="active">Uploaded! Back Home?</a></li> <li><a href="http://www.andre1990.com/tos.php">TOS</a></li> <li><a href="http://www.andre1990.com/faq.php">FAQ</a></li> <li><a href="http://www.andre1990.com/contact.php">Contact Us</a></li> <li><a href="http://www.andre1990.com/donate.php">Donate</a></li> </ul> </span> </center> <div id="main"><center> <div id="side1"><br><BR><BR> <br /><strong>Uploaded!</strong><br /> <span class="small"> <br /> Direct download/view:<br /> <input type="text" size="28" onClick=select() value="<?php echo $final; ?>" READONLY><p /> Forum Code download/view:<br /><br /> <input type="text" size="38" onClick=select() value="[url]<?php echo $final; ?>[/url]" READONLY><p /> Delete Link:<br><br> <input type="text" size="38" onClick=select() value="[url]<?php echo ??? ?>[/url]" READONLY><p /> <a href="index.php">Upload another file?</a> </span> <div class="clear"></div></center> </div></CENTER> <br><center><span class="small">© andre1990.</span></center> <center><a href="http://www.facebook.com/pages/andre1990/186225441417890"><img src="images/facebook.ico"></a></center> </div> </div> <div class="clear"></div> </div> </body> <html> Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267662 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267665 Share on other sites More sharing options...
JKG Posted September 10, 2011 Share Posted September 10, 2011 dont bump the thread. unlink('/home/user/www/files/' . $_GET['file_name']); would go in delete.php. you could then use header() to redirect the user. Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267668 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 Apologies. But to print the link to them in the first place, Delete Link:<br /><br /> <input type="text" size="38" onClick=select() value="<?php echo $server."/delete"."/".$destination."/".$name; ?>" READONLY><p /> Would that suffice? I wasn't following with header() Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267669 Share on other sites More sharing options...
JKG Posted September 10, 2011 Share Posted September 10, 2011 you dont need it to an input as your not sending a form, just use a link. <a href="<?php echo $server; ?>/delete.php?file_name=<?php echo $file ?>">Delete</a> Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267670 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 Ok im with you thus far! <a href="<?php echo $server; ?>/delete.php?file_name=<?php echo $name ?>">Delete</a> Then in the delete.php unlink($server."/".$destination."/".$name;); ? Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267672 Share on other sites More sharing options...
JKG Posted September 10, 2011 Share Posted September 10, 2011 unlink($server."/".$destination."/".$_GET[file_name]); Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267678 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 When i do that, it produces this after clicking the delete link {It may seem like im being lazy, but i am trying & trying to learn at the same time.} Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267682 Share on other sites More sharing options...
JKG Posted September 10, 2011 Share Posted September 10, 2011 use the <?php ?> tags. maybe just do some starter tutorials... Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267683 Share on other sites More sharing options...
JKG Posted September 10, 2011 Share Posted September 10, 2011 and you will need to redeclare the variables $server and $destination, or just hard code it. Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267685 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 It just brings up a blank page at the moment, not as easy as i first thought. Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267687 Share on other sites More sharing options...
JKG Posted September 10, 2011 Share Posted September 10, 2011 of course it would, you have not printed anything. what it should have done was delete the file, if the file hasnt been deleted then you need to set up your server to show errors. Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267688 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 It didn't delete anything, the file is still there. Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267691 Share on other sites More sharing options...
ZulfadlyAshBurn Posted September 10, 2011 Share Posted September 10, 2011 you would need to change the permission for the file. Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267693 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 Im trying to install yum on the server as we speak because all files are apache owned, i think thats the issue. I'll report back soon! Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267696 Share on other sites More sharing options...
redarrow Posted September 10, 2011 Share Posted September 10, 2011 Use cmod and unlink it a file permission problem. <?php @chmod( $entry, 0777 ); @unlink( $entry ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267703 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 php real degree do i add that to delete.php? <?php @chmod( $entry, 0777 ); @unlink( $entry ); unlink($server."/".$destination."/".$_GET[file_name]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267707 Share on other sites More sharing options...
JKG Posted September 10, 2011 Share Posted September 10, 2011 your problem is that you have not declared what $server and $destination are. get rid of them. also, you cannot use http://www.andre.com you have to use the server root. suspecting you are on a standard linux it would be /home/yourusername/www/Uploads/ if you dont know, then do a <?php echo phpinfo(); ?> or declare it relatively from the directory the script is in. and you are using @chmod( $entry, 0777 ); @unlink( $entry ); incorrectly. Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267712 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 the site is www.uploadvillage.com Could you guys give me some sort of finalised script for delete.php, im out of my depth. We are on CentOS: var/www/vhosts/uploadvillage.com/httpdocs/Uploads/ ? (Thats the tree from the server root) Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267714 Share on other sites More sharing options...
JKG Posted September 10, 2011 Share Posted September 10, 2011 unlink("/var/www/vhosts/uploadvillage.com/httpdocs/Uploads/".$_GET['file_name']); Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267715 Share on other sites More sharing options...
andre1990 Posted September 10, 2011 Author Share Posted September 10, 2011 I added that new link in place of the old one and that still didn't delete the file, could it be the apache owner? Quote Link to comment https://forums.phpfreaks.com/topic/246839-file-deletion-through-link/#findComment-1267716 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.