Php 间歇性断开连接不起作用

Php 间歇性断开连接不起作用,php,zend-framework,delete-file,unlink,Php,Zend Framework,Delete File,Unlink,下面的代码允许我有时删除文件。我已经检查了文件和文件夹的权限,它们已经存在并被授予了正确的访问权限。有时当我按下移除按钮时;它删除文件,有时只是刷新页面,什么也不发生。我能做些什么使取消链接正常工作吗?我在下面的代码中遗漏了什么吗?这是在ZEND public function delimageAction() { $request = $this->getRequest(); if ($request->isPost()) { // Get the

下面的代码允许我有时删除文件。我已经检查了文件和文件夹的权限,它们已经存在并被授予了正确的访问权限。有时当我按下移除按钮时;它删除文件,有时只是刷新页面,什么也不发生。我能做些什么使取消链接正常工作吗?我在下面的代码中遗漏了什么吗?这是在ZEND

public function delimageAction()
{
    $request = $this->getRequest();

    if ($request->isPost()) {
        // Get the image name
        $imageName = $request->getParam('file');

        $old = getcwd();
        chdir(APPLICATION_PATH . "/../public/images/blog/"); // Change directory to the files
        fclose(APPLICATION_PATH . "/../public/images/blog/" . $imageName);

        // Delete it
        unlink(APPLICATION_PATH . "/../public/images/blog/" . $imageName)

        chdir($old); // Return to old directory
    }
    $this->_helper->redirector('blog', 'index');
}

删除两个
chdir
调用,因为它们没有任何用途;删除将导致错误的
fclose
。除此之外,您还需要检查错误日志以查看导致删除失败的原因,这可能与权限有关。您还可以检查
unlink
的返回值,因为如果不起作用,它应该返回false


正如评论中所暗示的,脚本中存在很大的安全漏洞,因为它允许恶意用户删除应用程序中的任何文件。您需要清理“file”参数,以确保提供的路径位于
public/images/blog/
文件夹中。

尝试一下:POST参数file=“../../private/index.php”(或类似文件)
public function delimageAction()
{
    $request = $this->getRequest();

    if ($request->isPost()) 
    {
        // Get the image name
        $imageName = $request->getParam('file');

        // Delete it
        unlink(APPLICATION_PATH . "/../public/images/blog/" . $imageName)

    }

    $this->_helper->redirector('blog', 'index');
}