如何使用url src images作为id从文件夹中删除图像?

时间:2019-06-10 07:35:33

标签: image codeigniter textarea unlink

我有一篇文章,并且要删除其中的图像时,也要从存储文件夹中删除该文章。并且我想通过获取文章中的src url图片来删除图片

这是我的文章:

<p><img class="img-responsive" src="http://localhost/coba//berkas/news/blog-1560149702190.jpg" alt="1.jpg" /></p>

我想得到这样的图像名称:

blog-1560149702190.jpg

该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要提取图像名称,然后将其删除。

$image_url = "http://localhost/coba//berkas/news/blog-1560149702190.jpg";
$image_name = substr( strrchr($url, '/'), 1 );

// Check that the is indeed a file and that that file is an image type.
if(is_file($image_name) && strpos(mime_content_type($image_name), 'image/') === 0 ) {
    unlink($image_name);
}

正如其他人所说,删除这样的图像会带来安全风险。在上面的代码中,我仅检查源URL实际上是图像类型的文件,但是,您需要执行其他检查以确保要删除的图像确实与文章相关联,并且文章已被删除。首先。