Delete files with CodeIgniter

1

I want to delete a BD record that contains an image. When doing such an action, also exclude this same image from the folder where it is. How do you do this?

    
asked by anonymous 01.07.2014 / 03:05

2 answers

4

The helper of CodeIgniter files let you delete files in a particular path:

$this->load->helper("file");
delete_files($caminho);

If you want to delete just one file, you'll be better served using the unlink () of PHP:

$caminhoParaFicheiro = '/caminho/para/ficheiro/bubu.jpg';

if (unlink($caminhoParaFicheiro)) {
     echo 'Bubu morreu';
}
else {
     echo 'Não foi possível matar o BuBu';
}

How can you call the PHP unlink function:

    
01.07.2014 / 03:13
1

To delete a file only:

<?php

$file = '/dir/src/arquivo.png';

if(unlink($file)){

echo 'Excluido com sucesso';

}else{

echo 'Erro ao excluir';

}

?>

To delete multiple files:

<?php

$dir = '/dir/src/'; //Irá excluir todos do diretório src

$open = opendir($dir);

while($read = readdir($open)){

if($read != '.' && $read != '..'){

$e = @unlink($dir.$read);

if($e){
echo 'Arquivo {$read} excluido com sucesso <br>';
}else{
echo 'Erro ao excluir o arquivo {$read} <br>';
}

}

}

?>
    
01.07.2014 / 05:54