PHP 取消链接函数帮助

发布于 2024-11-16 06:12:24 字数 1954 浏览 1 评论 0原文

我编写了一个 PHP 脚本来删除在网格视图中选择的文件。这是我第一次这样做。该脚本在我的本地开发计算机上运行良好,但我不知道这是否是正确的方法。我想了解删除文件时可能会遇到哪些问题以及如何修改它以防止出现问题。

我正在查看此页面以获得基本想法: http://www.php .net/manual/en/function.unlink.php

<?php
// get required includes
require_once(ROOT_PATH.'user/controls/snippets/error_messages.php');
require_once(ROOT_PATH.'user/controls/accordion/get_user_name.php');

// ------------------------------------------------------------
// DELETE SELECTED FILES
// ------------------------------------------------------------
if(isset($_POST['delete_file']) && isset($_POST['checked2']))
{
    $checked = array_map('intval',$_POST['checked2']);
    $delete_list = implode(", ", $checked);

    // DB: get file names to delete
    $get_file_names = mysqli_query($conn, "SELECT FileName FROM downloads WHERE DownloadId IN ($delete_list) AND UserName = '$user_name'")
    or die($dataaccess_error);

    // delete files from server
    while($row = mysqli_fetch_array($get_file_names))
    {
        $dir = DOWNLOAD_DIRECTORY;
        $file_name = $row['FileName'];
        $file_to_delete = $dir.$file_name;
        unlink($file_to_delete);
    }

    // DB: delete selected file references from db
    $delete_selected = mysqli_query($conn, "DELETE FROM downloads WHERE DownloadId IN ($delete_list) AND UserName = '$user_name'")
    or die($dataaccess_error);

    if(mysqli_affected_rows($conn) > 0)
    {
        $effected_rows = mysqli_affected_rows($conn);
        echo "<div class='msgBox2b noBorder'>SUCCESS: ($effected_rows) FILE(S) have been DELETED..</div>";
    }
}
elseif(isset($_POST['delete_file']) && !isset($_POST['checked2']))
{
    echo $msg_error;
}
?>

谢谢!

编辑:这样会更好吗?

$fh = fopen($file_to_delete, 'w') or die($failed_to_open_file);
fclose($fh);
unlink($file_to_delete);

I wrote a PHP script to delete files selected in a gridview. This is the first time I've done this. The script works fine on my local development machine but I don't know if this is the proper way to do it. I'd like to find out what possible problems can I run into when deleting files and how can I modify this to prevent problems.

I was looking at this page to get the basic idea: http://www.php.net/manual/en/function.unlink.php

<?php
// get required includes
require_once(ROOT_PATH.'user/controls/snippets/error_messages.php');
require_once(ROOT_PATH.'user/controls/accordion/get_user_name.php');

// ------------------------------------------------------------
// DELETE SELECTED FILES
// ------------------------------------------------------------
if(isset($_POST['delete_file']) && isset($_POST['checked2']))
{
    $checked = array_map('intval',$_POST['checked2']);
    $delete_list = implode(", ", $checked);

    // DB: get file names to delete
    $get_file_names = mysqli_query($conn, "SELECT FileName FROM downloads WHERE DownloadId IN ($delete_list) AND UserName = '$user_name'")
    or die($dataaccess_error);

    // delete files from server
    while($row = mysqli_fetch_array($get_file_names))
    {
        $dir = DOWNLOAD_DIRECTORY;
        $file_name = $row['FileName'];
        $file_to_delete = $dir.$file_name;
        unlink($file_to_delete);
    }

    // DB: delete selected file references from db
    $delete_selected = mysqli_query($conn, "DELETE FROM downloads WHERE DownloadId IN ($delete_list) AND UserName = '$user_name'")
    or die($dataaccess_error);

    if(mysqli_affected_rows($conn) > 0)
    {
        $effected_rows = mysqli_affected_rows($conn);
        echo "<div class='msgBox2b noBorder'>SUCCESS: ($effected_rows) FILE(S) have been DELETED..</div>";
    }
}
elseif(isset($_POST['delete_file']) && !isset($_POST['checked2']))
{
    echo $msg_error;
}
?>

Thank you!

Edit: Would it be better this way?

$fh = fopen($file_to_delete, 'w') or die($failed_to_open_file);
fclose($fh);
unlink($file_to_delete);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

善良天后 2024-11-23 06:12:24

由于权限的原因,并非所有文件都可以取消链接,因此请检查该调用的返回值。

Not all files can be unlinked because of permissions, so check the return value of that call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文