unlink() as Delete only one file from the file

Asked

Viewed 931 times

1

I’m having trouble deleting a file from the file, when I pass a "GET", it deletes all files from the file 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';
   }
?>
  • It’s kind of weird this code, there’s a return $del->execute() then a unlink() ?

  • where the $resphotos['id']?

  • resphotos is from the album class @Rafael Acioly is the function that selects in the table

  • @rray this Return is to delete the BD file I am beginner in php, mainly in PHPOO

  • Return will cancel all lines of code below it, ie it leaves the function

  • @rray I removed the Return , but when I delete the photo does not erase the right photo, delete the first uploaded photo from the album, and so on

Show 1 more comment

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 the files because it is inside a while loop that traverses all the files within the directory. If you want to delete only one file, you should not use unlink() within the while.

I believe you don’t even need a while loop, since you just want to delete a single file. Simply pass it as parameter to unlink() should already solve your problem.

Any questions just ask.

  • So , @pauloequilibrio thanks for your reply, I read an article about the while function and saw that while is a repititiva function something like that , immediately I removed it, tried to delete a photo , and unlink does not erase the right image

  • But are you telling unlink() what is the right image? : unlink in php.net

  • in the variable folder I concatrenei the photo id ex $resphotos['photo']

  • OK, but the file name is just the photo id? Don’t have a . jpg extension? What should be informed the unlik() function is the file name exactly as seen by the file system and not by the database.

  • @pauloequilibrio my upload system is compatible with all formats (.bmp.jpeg.pjpeg.png.gif) as I declare these formats

  • @Php give me a more concrete example of what you want to do, like give me the name of a photo you want to delete and the information from it that is stored in your database, so I can give you an example of code that solves your problem.

  • Use var_dump() to see what comes out in your variables so you debug line by line if unlinlk() does not delete is pq does not find the correct path or file

  • +1, the answer was straight to the problem. The loop is giving unlink on all, there is no condition saying to delete only a few. In fact the original loop has a disproportionate complexity with the task to be performed.

Show 3 more comments

Browser other questions tagged

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