www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] Delete previously uploaded file from server?

  1. #1
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265

    resolved [RESOLVED] Delete previously uploaded file from server?

    Ok, so I have a page where an authorized user can upload a file... there is also a list of uploaded files (each has info in the database) and a "delete" check-box to remove it... It removes the info from the database, but how do I remove the file itself from the "docs" folder on the server?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,327
    unlink()
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265
    I tried that, but it's not working. There's a "close file" thingy that gets put in before that tries to open the file and returns the error "can't open file" -- probably because the file is a PDF... should the file-type be causing a problem with the unlink() function?

    Here's what I've got to delete the file:
    PHP Code:
        $delFile "docs/".$rowfile;
        
    $fh fopen($delFile'w') or die("can't open file");
        
    fclose($fh);
        
    unlink($delFile); 
    ...and it just returns the "can't open file" error.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,327
    Well, if it's dieing on that line, then the following lines never get executed. So you might want to add some debug code to find out why the fopen is failing.
    PHP Code:
    $delFile "docs/".$rowfile;
    if( ! 
    file_exists($delFile)) {
       die(
    "Cannot find file '$delFile'");
    }
    if( ! 
    is_writable($delFile)) {
       die(
    "File '$delFile' is not writable");
    }
    ini_set('display_errors'true);
    error_reporting(E_ALL);
    $fh fopen($delFile'w') or die("can't open file");
    fclose($fh);
    unlink($delFile); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265
    Error message:
    Code:
    Warning: fopen(docs/) [function.fopen]: failed to open stream: Is a directory in /home/mywebsite/public_html/sites/misc/testfolder/adm_docs.php on line 142
    can't open file

  6. #6
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Sounds like $rowfile isn't defined ...
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  7. #7
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265
    Except that "$rowfile" is created just a few lines up from the filename from the database. It should work, but it doesn't. I'm going to try tweaking a couple of other things & then I'll get back to you guys. Thanks a ton for the help thus far.

  8. #8
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265
    The script runs! Now i just have to fix the file-uploader to work properly so I can test this correctly!

    Thanks!

    The problem was this:
    Code:
    $delFile = "docs/".$rowfile;
    The solution was this:
    Code:
    $delFile = "/docs/".$rowfile;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles