Clear directory and then create gallery inside it using php

Asked

Viewed 34 times

0

Good afternoon, I’m creating a file that edits the products' post, only before editing the data and inserting the new ones the customer selected. was to delete the photos from the directory where the image gallery was created, would like help from vcs, follows the code commented.

    <?php
// Incluir o arquivo conecta.php que faz a conexão com o banco de dados
include "conecta.php" ;
$Nomediretorio = $_POST['titulo'];

//apagando os dados antes de iniciar a edição

function apaga_files($name)
{
  $dir = 'foto_produtos/galeria/'.$Nomediretorio;
  if(is_dir($dir))
  {
    if($handle = opendir($dir))
    {
      while(($file = readdir($handle)) !== false)
      {
        if($file != '.' && $file != '..')
        {
          if( $file != $name)
          {
            unlink($dir.$file);
          }
        }
      }
    }
  }
  else
  {
    die("Erro ao abrir dir: $dir");
  }
        return 0;
}

//inicia a edição

$id = $_POST['i'];
$imagem = $_FILES['imagem'];

//pega extenção da imagem

preg_match("/\.(gif|bmp|png|jpg|jpeg|){1}$/i",$imagem['name'], $ext);

$nome_imagem = md5(uniqid(time())) . ".". $ext[1];

$caminho_imagem = "foto_produtos/".$nome_imagem;

move_uploaded_file($imagem['tmp_name'], $caminho_imagem);


$sql = "UPDATE portifolio SET imagem = '".$nome_imagem."', titulo = '".$_POST['titulo']."' ,
 descricao = '".$_POST['descricao']."', marca = '".$_POST['marca']."',
 referencia = '".$_POST['referencia']."', preco = '".$_POST['preco']."', destaque = '".$_POST['destaque']."',
 ativa = '".$_POST['ativa']."', data = now() WHERE id = $id";
$query =  mysql_query($sql) or die (mysql_error());

//Retorno a página de formulário
echo "
 <script language='javascript'>
 alert('Dados editados com sucesso!');
 parent.location='portifolio_adm.php';
     </script>
";
?>
  • good is trying to delete directory?

  • I don’t just want to clean the files inside it, the rest of the code works fine. just the part about cleaning the records

  • You have checked if the directory exists with file_exists within the function apaga_files?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.