简体   繁体   中英

PHP / PDO : Delete image on server

The purpose of the following script is to delete an entry in my annonces table using provided "id".

I would like also delete a file with name $imageAnnonce which is stored in the following folder : /uploads

How can i do that using PHP ?

<?php

$PARAM_hote='aaaaaaaa'; 
$PARAM_port='3306';
$PARAM_nom_bd='bbbbbbbbbbb'; 
$PARAM_utilisateur='cccccccccccc'; 
$PARAM_mot_passe='ddddddddddddd';
// Create connexion to BDD
$connexion = new PDO('mysql:host='.$PARAM_hote.';port='.$PARAM_port.';dbname='.$PARAM_nom_bd, $PARAM_utilisateur, $PARAM_mot_passe);

try {

    // GET POST DATA
    $idAnnonce = $_POST['idAnnonce'];
    $imageAnnonce = $_POST['imageAnnonce'];

    // PREPARE DELETE ON TABLE
    $sqlInsert = "DELETE FROM annonces WHERE id=:idAnnonce ";
    $resultats = $connexion->prepare($sqlInsert);
    $resultats->bindValue(':idAnnonce', $idAnnonce, PDO::PARAM_INT);
    $resultats->execute();

    // Now, i would like to delete image with name = $imageAnnonce in the folder /uploads
    // ... ??

    // How many row have been impacted ?
    echo $resultats->rowCount();

} catch(Exception $e) {
    echo 'Erreur : '.$e->getMessage().'<br />';
    echo 'N° : '.$e->getCode();
}



?>

Use unlink to delete a file:

unlink(pathtofile);

So in your case:

unlink ('uploads/'.$imageAnnonce);

Try unlink() function: http://php.net/manual/fr/function.unlink.php .

For you, it will be:

unlink("uploads/$imageAnnonce");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM