0

下面的代码允许我有时删除文件。我已经检查了文件和文件夹的权限,它们存在并获得了适当的访问权限。有时当我按下删除按钮时;它会删除文件,有时它只是刷新页面而没有任何反应。我可以做些什么来使 unlink 正常工作吗?我在下面的代码中遗漏了什么吗?这是在 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');
}
4

2 回答 2

2

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

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

于 2012-11-27T10:31:09.287 回答
0
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');
}
于 2015-08-24T18:00:48.407 回答