Search code examples
phpunlink

php unlink file


I've such a situation. I want to delete .wav file which is out of webroot directory , but I've defined in httpd.conf (apache) alias of this directory like "mp3" . this is working nicely, because I can download file from webroot and so on ... But I want to delete it also which I can't do. I have PHP script like this =>

class Delete{
   public function del_directory_record($filename){
    if (unlink("/mp3/$filename")){
        return true;
    }
}
}

 $new = new Delete();
 $new->del_directory_record("file.wav");

In php-errors it gives me "PHP Warning => No such file or directory" I'm interested in what I'm doing wrong ?

It still doesn't work ...

I've at C:\server\webroot... and I have directory mp3_files at C:\server\mp3_files In httpd.conf I've written

Alias /mp3/ "C:/server/mp3_files/"
<Directory "C:/server/mp3_files/">
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Directory>

Solution

  • I think you meant to do this relative to your DOCUMENT_ROOT:

    class Delete {
       public function del_directory_record($filename) {
          return unlink($_SERVER['DOCUMENT_ROOT'] . "/mp3/$filename");
       }
    }
    
    $new = new Delete();
    $new->del_directory_record("file.wav");
    

    Just use this standalone function, it will do just fine. No need to create an object or class.

    function delete_directory_record($filename) {
       return unlink($_SERVER['DOCUMENT_ROOT'] . "/mp3/$filename");
    }