[英]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.