簡體   English   中英

PHP / PDO:刪除服務器上的圖像

[英]PHP / PDO : Delete image on server

以下腳本的目的是使用提供的“ id”刪除我的匿名表中的條目。

我還想刪除一個名稱為$ imageAnnonce的文件,該文件存儲在以下文件夾中:/ uploads

我如何使用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();
}



?>

使用unlink刪除文件:

unlink(pathtofile);

因此,在您的情況下:

unlink ('uploads/'.$imageAnnonce);

嘗試unlink()函數: http : //php.net/manual/fr/function.unlink.php

對於您來說,它將是:

unlink("uploads/$imageAnnonce");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM