Matt
Matt

Reputation: 3680

PHP or Apache Delete Files

I have an asterisk server that is placing call recordings in the /var/spool/asterisk/monitor directory. I have managed to get Apache (httpd) to display a list of everything in this directory.

I have a secondary process that is coming along and downloading those recordings and doing something with them. When that process is done I want it to be able to DELETE the recording it pulled.

If I have to use PHP I can, but I have already tried that and do not know enough to get it to work. I am trying the code below, I'm guessing its a permissions issue or something, but I don't know how to change the apache user to run as a higher level user (root would be fine, and yes I'm aware of the ramifications).

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php

        if($_GET['action'] == 'delete')
        {
                $myfile = $_GET['filename'];
                $path = getcwd();
                echo($path . "/" . $myfile);
                if(unlink($path . "/" . $myfile))
                {
                        echo('Success!!!');
                }
                else
                {
                        echo('Failure');
                }
        }
        else
        {
                echo('doing nothing');
        }

?>
</body>
</html>

The other option that I would be ok with would be the ability to do a HTTP DELETE verb against the file and let Apache handle it, but I can't find a good article on how to set that up either.

This is the entry I have in httpd.conf file

Alias /recordings "/var/spool/asterisk/monitor"
<Directory "/var/spool/asterisk/monitor">
        Options Indexes FollowSymLinks MultiViews ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
</Directory>

Please help.

Upvotes: 0

Views: 3584

Answers (2)

hakre
hakre

Reputation: 198118

Next to a GET request to retrieve the file, you could do a DELETE request to delete the file.

By default, Apache has not request handler for the DELETE method, but you can create your own delete.php that takes care of the DELETE requests:

<?php
/**
 * delete script
 */
if ($_SERVER['REQUEST_METHOD'] !== 'DELETE') {
    header('Method not allowed', 405);
    exit;
}
$basepath = __DIR__;
$file = basename($_SERVER['REQUEST_URI']);
$path = $basepath . '/' . $file;
$exists = file_exists($path);
if (!$exists) {
    header($_SERVER['SERVER_PROTOCOL'] . ' 204 No Content'); // HTTP/1.1 ...
    exit;
}
$success = unlink($path);
if ($success) {
    printf("Deleted %s\n", $file);
} else {
    header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
}

In your .htaccess file you then map all DELETE requests to your deletion script:

RewriteEngine On
RewriteCond %{REQUEST_METHOD} =DELETE [NC]
RewriteRule .* delete.php

The benefit of such a script is, that it correctly communicates the success or failure via the HTTP status codes which are defined: Hypertext Transfer Protocol -- HTTP/1.1 (RFC2616) Section 9.7 DELETE.

Upvotes: 1

zod
zod

Reputation: 12437

Why you are putting "\\" ? the path will not have that double slash is it!

unlink is pretty staright forward

For confirming the path is correct .. try to open the file using fopen . if it works . delete it with unlink

http://php.net/manual/en/function.unlink.php

check file exist and delete it

http://php.net/manual/en/function.file-exists.php

Check file permission and your privileges to delete

http://us.php.net/fileperms

Upvotes: 0

Related Questions