dongmao9217 2014-06-11 13:25 采纳率: 100%
浏览 58
已采纳

PHP / PDO:删除服务器上的图像

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();
}



?>
  • 写回答

2条回答 默认 最新

  • dpm91915 2014-06-11 13:30
    关注

    Use unlink to delete a file:

    unlink(pathtofile);
    

    So in your case:

    unlink ('uploads/'.$imageAnnonce);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用
  • ¥15 ESP-IDP-BLE配网连接wifi
  • ¥15 ue2.6.12版本用的若以,安装gojs,引入import * as go from 'gojs';报错
  • ¥15 服务器上的网站安装php5.6版本