php unlink file
Asked Answered
G

2

5

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>
Grate answered 4/1, 2012 at 13:6 Comment(0)
S
10

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");
}
Stoddart answered 4/1, 2012 at 13:8 Comment(4)
Thanks for helping :) . I know that only this script don't belong to class , but it is only part class script ...Grate
It still doesn't work :(, Now it writes me the same No such file or directory. I can't guess whats wrong with itGrate
@Grate Where on your filesystem does "file.wav" live relative to your web server's document root?Stoddart
In webroot directory I've not problem, everthing working nicely, but outside of webroot it doesn'tGrate
I
-3

Try

if (unlink("/mp3/".$filename)){
        return true;
    }

or what Jacob Relkin wrote with document root

Irrelievable answered 4/1, 2012 at 13:8 Comment(2)
php uses a period to concatenate strings, not a plus. Just an FYI.Kobarid
sorry, I forgot about string concat in PHP :PIrrelievable

© 2022 - 2024 — McMap. All rights reserved.