【问题标题】:Delete file from upload path with database table使用数据库表从上传路径中删除文件
【发布时间】:2011-10-08 07:00:53
【问题描述】:

我使用以下代码从数据库中删除了图像 url,但不是从上传路径中删除文件图像,如何从上传路径中删除带有数据库中的 url 的文件(两者一起)?

此 php 仅用于从数据库中删除 url:

$delete = $this -> input -> post('checked');
foreach($delete as $val) {
    $this - > db - > delete($this - > _table, array('id' = > $val));
}

更新:

我在使用以下代码时出错。

$id = '166';
$query = $this - > db - > get_where('hotel_image', array('id' = > $id));
if ($query - > num_rows() > 0) {
    $res = $query - > row();
    unlink(base_url(uploads/$res - > images));//This is line 161
}
$this - > db - > delete('hotel_image', array('id' = > $id));

错误:(我的图片在这个pach中:http://example.com/uploads/

遇到 PHP 错误
严重性:通知
消息:使用 未定义的常量上传 - 假定为“上传”
文件名:user/dence.php
行号:161

遇到 PHP 错误
严重性:警告
消息: unlink() [function.unlink]: http 不允许取消链接
文件名:user/dence.php
行号:161

【问题讨论】:

    标签: php codeigniter file-upload delete-file


    【解决方案1】:

    您需要使用unlink()从磁盘中删除文件,只需将文件名作为参数传递。

    bool unlink (string $filename [, resource $context])

    删除文件名。类似于 Unix C unlink() 函数。一个 E_WARNING 失败会产生级别错误。

    类似的东西(这是简化的代码。您可能需要检查来自 DB 的路径是否完整,或者添加其余部分。):

    $delete = $this -> input -> post('checked');
    foreach($delete as $val) {
        $this->db->select('images')->from($this->_table)->where('id',$val);
        $query = $this->db->get();
        if($query->num_rows() > 0)
        {
          $res = $query->row();
          $file = './uploads/'.$res - > images));
          // echo $file;  #check if this is the correct path!
          @unlink($file);
        }
        //now query to delete
        $this -> db -> delete($this - > _table, array('id' = > $val));
    }
    

    【讨论】:

    • 我在使用此功能时出错,请在我的第一篇文章中查看更新并告诉我!!?
    • @EmmyCharles 编辑了我的答案。你没有建立正确的路径。更新:另外,如果您使用完整路径,请检查您的 INI 设置是否允许。万一(我重新更正了我的答案)使用相对路径
    猜你喜欢
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2023-03-27
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多