Need help w/ Unlink() function

Discussion in 'PHP' started by exponent, Mar 6, 2007.

  1. #1
    Hello, I need some help with an unlink function. I'm getting the following errors when I try to delete / unlink a file.

    The code is below, starting at line 41. Any help would be much appreciated. This is for my site Upload4Free.com If anyone can help me fix this, I would gladly tip you.

    
    if(isset($_GET['delete'])) {
    
    unlink("./files/".$_GET['delete'].".txt");
    unlink("./storage/".$_GET['delete']);
    
    if(isset($_GET['banreport'])) {
    
    $bannedfile = $_GET['banreport'];
    if (file_exists("./files/$bannedfile".".txt")) {
    	unlink("./files/".$bannedfile.".txt");
    	unlink("./storage/".$bannedfile);
    	$deleted=$bannedfile;
    }
    $fc=file("./reports.txt");
    $f=fopen("./reports.txt","w+");
    foreach($fc as $line)
    {
    Code (markup):

     
    exponent, Mar 6, 2007 IP
  2. thejared

    thejared Peon

    Messages:
    382
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I have a script that used the unlink function, it was written like this:

    I don't know why yours has $_GET in it. I'm not a php genius, I just saw the unlink function and figured I'd throw in my 2 cents.
     
    thejared, Mar 6, 2007 IP
  3. exponent

    exponent Peon

    Messages:
    1,243
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks =) The unlink has to call up a file.. basically, the site has a list of downloads that have been reported.. i click a button to delete the file, but it has to retrieve the file, so it uses $_GET

    I'll probably just switch to another script if I cant get this thing working correctly.
     
    exponent, Mar 6, 2007 IP
  4. PhatDV

    PhatDV Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Using isset() is a bad idea when dealing with file names. For example the URL:

    http://www.website.com/admin.php?delete=

    will cause your if statement to become true and your unlinks will run.

    A better approach would be to check the length of the 'delete' statement using strlen().

    Even better would be to check that the file actually exists before trying to delete it, using file_exists() as well. Could be improved by using is_writable() as well.

    Also, keep in mind that since you're using unlink, make sure that the stuff being passed via 'delete' is actually something you want to be deleted. Imagine what would happen if someone set 'delete' to '../admin.php' - DON'T TRY THIS YOURSELF, YOU'LL LIKELY DELETE YOUR admin.php FILE!!
     
    PhatDV, Mar 7, 2007 IP
  5. Jim_

    Jim_ Peon

    Messages:
    72
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I know the script you're using. :p

    In your admin panel, what is the url of the delete link you're clicking? It looks like you're clicking on a blank report request. Check if there is a blank line or something in reports.txt
     
    Jim_, Mar 7, 2007 IP
  6. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Using the following code will most probably prevent the error.
    
    if(!empty($_GET['delete'])) {
    
    	unlink("./files/".$_GET['delete'].".txt");
    	unlink("./storage/".$_GET['delete']);
    
    
    Code (markup):
    But our problem is that $_GET['delete'] is empty. Can you plz give the url of the page showing the above error? Just to check whether delete={filename} exists or not.
     
    Aragorn, Mar 7, 2007 IP
  7. exponent

    exponent Peon

    Messages:
    1,243
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Its my admin page for Upload4Free.com
     
    exponent, Mar 7, 2007 IP