使用数据库表从上传路径中删除文件

发布于 2024-12-09 04:31:28 字数 996 浏览 0 评论 0原文

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

此 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

I done delete image url from database with following code, but is not delete file image from upload path, how can delete file from upload path with url it in database (both together)?

This php is just for delete url from database:

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

Update:

I have error in use of following code.

$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));

Error:(my images are in this pach: http://example.com/uploads/)

A PHP Error was encountered
Severity: Notice
Message: Use of
undefined constant uploads - assumed 'uploads'
Filename: user/dence.php
Line Number: 161

A PHP Error was encountered
Severity: Warning
Message:
unlink() [function.unlink]: http does not allow unlinking
Filename: user/dence.php
Line Number: 161

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

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

发布评论

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

评论(1

云淡风轻 2024-12-16 04:31:28

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

布尔取消链接(字符串$文件名[,资源$上下文])

删除文件名。类似于 Unix C unlink() 函数。 E_警告
失败时会产生级别错误。

类似的东西(这是简化的代码。您可能想检查数据库的路径是否完整,或者添加其余部分。):

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

You need to use unlink() to delete a file from disk, just pass the filename as parameter.

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

Deletes filename. Similar to the Unix C unlink() function. A E_WARNING
level error will be generated on failure.

Something like (this is simplified code. You might want to check the path from DB is complete or else add the rest.):

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