unlink () How to Delete only one file from the file

1

I'm having difficulty erasing a file from the file, when I pass a "GET", it deletes all files from the file do I have to pass some variable to identify the file name?

<?php
    if(isset($_SERVER['REQUEST_METHOD']) AND $_SERVER['REQUEST_METHOD']=='GET'){
        $pasta = 'uploads/photos/';
        if(is_dir($pasta)){
            $diretorio = dir($pasta);
            while($arquivo = $diretorio->read()){
                $arquivo = ''.$resphotos['photo'].''; 
                if(($arquivo!='.')&&($arquivo!='..')){
                    $id =  $arquivo = ''.$resphotos['id'].''; 
                    $del = DB::getConn()->prepare('DELETE FROM 'photos' WHERE 'id'=? LIMIT 1');
                    return $del->execute(array($id));  
                    unlink($pasta.$arquivo);
                    echo'<span> o arquivo foi apagado</span>';
                }
           }
       }
        $diretorio->close();
   }else{
    echo'a pasta nao existe';
   }
?>
    
asked by anonymous 25.09.2015 / 20:41

1 answer

1

The answer to your question is yes! You have to indicate somehow which is the only file you want to delete.

Your code is deleting all files because it is within a while loop that traverses all files within the directory. If you want to delete only one file, you should not use unlink() within while .

I do not think you even need a while loop, since you just want to delete a single file. Simply passing it as a parameter to unlink() should already solve your problem.

Any questions just ask.

    
26.09.2015 / 17:22