0

Eu criei uma maneira de eliminar múltiplos arquivos de uma pasta via MySQL.

Variáveis do MySQL:

   $location_files = $listed['ads_image_1'];                    
   $location_files2 = $listed['ads_image_2'];                    
   $location_files3 = $listed['ads_image_3'];

Array onde vai polos:

   $remove_files = array($location_files, $location_files2, $location_files3);

Função onde vai elimina-los:

   if(file_exists($remove_files)) {

                      $files = glob($remove_files); // get all file names
                         foreach($files as $file){ // iterate files
                           if(is_file($file))
                              unlink($file); // delete file
                      }   
               }   

Mas aconteceu um erro. Alguém poderia ajudar?

Erro:

  Warning: file_exists() expects parameter 1 to be a valid path, array given in D:\xampp\htdocs\Superfacil_v1.7.9\myads.php on line 819 (if(file_exists)...

1 Resposta 1

2

Tanto a função file_exists quanto a função glob, devem receber uma string. Como você está passando um array, o erro está sendo mostrado.

Uma alternativa é utilizar um foreach, por exemplo:

<?php

$remove_files = array($location_files, $location_files2, $location_files3);

foreach($remove_files as $location) {

    if(file_exists($location)) {
        $files = glob($location); // get all file names

        foreach($files as $file){ // iterate files
            if(is_file($file))
                unlink($file); // delete file
        }   
    }   
}

Ou você poderá utilizar uma função de callback com a array_map, por exemplo:

<?php

function removeFiles($location) {
    if(file_exists($location)) {
        $files = glob($location); // get all file names

        foreach($files as $file){ // iterate files
            if(is_file($file))
                unlink($file); // delete file
        }   
    }  
}

$remove_files = array($location_files, $location_files2, $location_files3);

array_map('removeFiles', $remove_files);
1
  • Muito agradecido! Era isto mesmo que estava-me a falhar! Já ponho Boa Resposta.. de-me 1min 15/04/2018 às 17:14

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .