简体   繁体   English

Codeigniter Unlink不起作用

[英]Codeigniter Unlink doesn't work

I want to delete an image before deleting an entry so I did this 我想在删除条目之前删除图像,所以我这样做了

$query = $this->db->get_where('info', array("id" => $info_id));
        $results = $query->result_array();
        echo base_url().'theme/transport/images/services/thumbs/' . $results[0]['image'];die();
        unlink(base_url().'theme/transport/images/services/thumbs/' . $results[0]['image']);

But the file is still there. 但文件仍然存在。 It doesn't unlink. 它没有取消链接。 What am I doing wrong? 我究竟做错了什么?

Use 采用

unlink('theme/transport/images/services/thumbs/' . $results[0]['image']);

Instead of 代替

unlink(base_url().'theme/transport/images/services/thumbs/' . $results[0]['image']);

Thanks 谢谢

First remove die() it terminates execution of program.Then try 首先删除die()它终止程序的执行。然后尝试

        $query = $this->db->get_where('info', array("id" => $info_id));
        $results = $query->result_array();
        echo base_url().'theme/transport/images/services/thumbs/' . $results[0]['image'];
        unlink(base_url().'theme/transport/images/services/thumbs/' . $results[0]['image']);

Because you are trying to unlink a file. 因为您正在尝试取消链接文件。 You should try use FCPATH 你应该尝试使用FCPATH

unlink(FCPATH .'theme/transport/images/services/thumbs/' . $results[0]['image']);

path functions definitions 路径函数定义

EXT: The PHP file extension
FCPATH: Path to the front controller (this file) (root of CI)
SELF: The name of THIS file (index.php)
BASEPATH: Path to the system folder
APPPATH: The path to the "application" folder

You can also try out by this way. 您也可以通过这种方式尝试。 Make sure the image file is reachable with the path from the location you execute the PHP script. 确保使用您执行PHP脚本的位置的路径访问映像文件。 I'm using realpath() to get the file location. 我正在使用realpath()来获取文件位置。

$images_location = realpath('theme/transport/images/services/thumbs/' . $results[0]['image']);
if (file_exists($images_location))
{
   unlink($images_location);
}

Please let me know if there any problem. 如果有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM