PHP或Apache删除文件 [英] PHP or Apache Delete Files

查看:150
本文介绍了PHP或Apache删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将通话录音在/ var /阀芯/星/监控目录中的星号服务器。我设法让阿帕奇(httpd的),此目录中显示的所有内容的列表。

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.

如果我必须使用PHP我可以,但我已经试过了,不知道够得到它的工作。我想在code以下,我猜测它的权限问题什么的,但我不知道如何改变apache用户为更高级别的用户身份运行(根目录就可以了,是的,我'米意识到后果的)。

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>

另一种选择,我将是确定有会做一个HTTP DELETE动词对文件,让Apache的处理它的能力,但我不能找到如何设置了无论是好文章。

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.

这是我有条目httpd.conf文件

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>

请帮忙。

推荐答案

旁边有一个 GET 请求来检索文件,你可以做一个删除要求删除该文件。

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

在默认情况下,阿帕奇并没有要求为删除方法处理,但你可以创建自己的 delete.php 这需要照顾的删除的请求:

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');
}

在你的的.htaccess 文件,你再映射所有删除要求你删除脚本:

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

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

这样一个脚本的好处是,它正确地通信通过HTTP状态codeS被定义的成功或失败:超文本传输​​协议 - HTTP / 1.1(RFC2616)第9.7节DELETE

这篇关于PHP或Apache删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆