Php 删除数据库列后删除文件夹
我在一个数据库表中存储属性详细信息,在另一个数据库表中存储属性图像。删除属性后,我将从数据库中删除图像和属性本身。但是我还需要从服务器上删除图像。而我在做这件事时遇到了麻烦。。我的数据库中的图像如下所示Php 删除数据库列后删除文件夹,php,codeigniter,Php,Codeigniter,我在一个数据库表中存储属性详细信息,在另一个数据库表中存储属性图像。删除属性后,我将从数据库中删除图像和属性本身。但是我还需要从服务器上删除图像。而我在做这件事时遇到了麻烦。。我的数据库中的图像如下所示 FILE_PATH | PROP_ID 在文件路径中,它是这样存储的 /images/uploads/2016/01/propname/filename.jpg 但是完整路径/var/www/html/images/uploads/2016/01/vsehrdova-6s/vsehrdova
FILE_PATH | PROP_ID
在文件路径中,它是这样存储的
/images/uploads/2016/01/propname/filename.jpg
但是完整路径/var/www/html/images/uploads/2016/01/vsehrdova-6s/vsehrdova-6s-1.jpg
function delete_single_property($id)
{
$this->db->delete('properties', array('room_id' => $id));
$this->db->delete('properties_images', array('PROP_ID' => $id));
}
您将无法删除该文件夹,因为您没有将其存储在数据库中。我可以帮助您删除服务器中的图像文件。。这应该起作用:
function delete_single_property($id)
{
$q = $this->db->select('FILE_PATH')->from('properties_images')->where('PROP_ID', $id)->get();
$dbase_path = $q->result_array();
$new = array();
foreach ($dbase_path as $single) {
$new[] = '/var/www/html' . $single['FILE_PATH'];
}
foreach ($new as $file) { // iterate files
if (is_file($file))
unlink($file); // delete file
}
$this->db->delete('properties', array('room_id' => $id));
$this->db->delete('properties_images', array('PROP_ID' => $id));
}
它将循环属性检查的所有文件路径,检查您存储在数据库文件路径中的内容是否确实存在,以及是否存在一个文件。而
取消链接
只需删除文件您存储的是文件名而不是文件夹名。。如果您删除文件夹中的每个图像文件会更好,除非您将文件夹完整路径存储在数据库中,否则无法删除文件夹。@IlanHasanov您能帮我删除文件吗?他可以使用dirname()函数,该函数返回文件的目录。它实际上正在工作。我认为删除文件比删除文件夹更好。我怎么能把它和我的功能结合起来呢?我不知道CodeIgniter
function delete_single_property($id)
{
$q = $this->db->select('FILE_PATH')->from('properties_images')->where('PROP_ID', $id)->get();
$dbase_path = $q->result_array();
$new = array();
foreach ($dbase_path as $single) {
$new[] = '/var/www/html' . $single['FILE_PATH'];
}
foreach ($new as $file) { // iterate files
if (is_file($file))
unlink($file); // delete file
}
$this->db->delete('properties', array('room_id' => $id));
$this->db->delete('properties_images', array('PROP_ID' => $id));
}