tags:

views:

26

answers:

1

I'm trying to delete a file on the server. Below is the code I use.

function ServerDel($file){
        $file = realpath($file);
        echo ($file);
        $fh = fopen($file, 'w') or die("can't open file");
        fclose($fh);
        if(unlink($file))
            echo"Delete the file successfully.";
        else
            echo "Failed to delete.";
}

But after I run the code, the file still exists and becomes 0KB. Anyone knows how to get around this?

Thanks in advance.

+1  A: 
Ruel
Thanks for the quick response. I changed the flag and now I get "Failed to delete." msg.
RJia
I'm running on Windows 2003, if that matters. What is the tag for?
RJia
actually, the `w` flag truncates the file to zero length. I'm editing my answer.
Ruel
Thanks for trying to help me. I tried the new code but still get failed to delete error..
RJia
can you please show me how you are using your `ServerDel` function?
Ruel
I had a switch loop. when detecting the file is stored on server, i call the ServerDel function. The echoed value of $rfile is sth. like c:\Inetpub\wwwroot\temp\File Name.PDF
RJia
How are you running the script? Through command line? Maybe because you don't have permissions.
Ruel
It's a web app, running from IE. I gave full permissions to everyone.
RJia
I just tried again and it works!! Maybe there was some caching. Thanks a lot for your help, Ruel.
RJia