删除和修改数据库的问题

时间:2014-05-11 17:20:08

标签: php

我正在努力:

  1. 上传图片
  2. 删除旧图片
  3. 修改数据库中新图片的文件名。
  4. 请注意,$foto的值位于lock.php,代表图片文件名。

    lock.php:

    $ses_sql=mysql_query("select user, name, email2, email, lname, sex, pic1 from miembros    where user='$user_check' ");
    $row=mysql_fetch_array($ses_sql);
    $login_session=$row['user'];
    $foto=$row['pic1'];
    

    这是我的代码changeprofilepic.php:

    include('lock.php');
    
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    $foto = $_FILES["file"]["name"];
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 2000000)
    && in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
    echo "C&oacute;digo de retorno: " . $_FILES["file"]["error"] . "<br>";
    } else {
    echo "<p align='center'><img src='fotoperfil/$foto' width='300'/></p>";  
    echo "<b>Archivo:</b> " . $_FILES["file"]["name"] . "<br>";
    echo "<b>Tipo de Imagen:</b> " . $_FILES["file"]["type"] . "<br>";
    echo "<b>Tama&ntilde;o:</b> " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    if (file_exists("fotoperfil/" . $_FILES["file"]["name"])) {
      echo "<p style='color:red'><b>Error:</b> $foto ya existe! Por favor elija otro nombre de archivo.</p>";
      echo "<p align='center'><button class='bon' onclick='goBack()'>Ir atr&aacute;s</button><input type=button class='bon' onClick='location.href=\"mailto:info@example.org\"' value='Contactar al Administrador'></p>";
    } else {
    
    if(is_file($_SERVER['DOCUMENT_ROOT'] . "fotoperfil/$foto") && $foto != 'nopic.png')      {
       // remove the old file 
    unlink($_SERVER['DOCUMENT_ROOT'] . "fotoperfil/$foto");
    }
    // move the new file *after* the unlink
    move_uploaded_file($_FILES["file"]["tmp_name"],"fotoperfil/" . $_FILES["file"]["name"]);
    // If I understand your comment: update the $foto value with new filename
    $foto = $_FILES["file"]["name"];
    mysql_query("UPDATE miembros SET pic1 = '$foto' WHERE user = '$login_session'"); 
    

    问题是代码会删除我刚上传的相同图片。我的目标是删除旧图片。

1 个答案:

答案 0 :(得分:0)

您可以在移动新上传的文件之前删除旧文件。您还可以检查是否存在旧文件。

include('lock.php');
// If I got an old file AND still exists on FS
if(is_file($_SERVER['DOCUMENT_ROOT'] . "fotoperfil/$foto") && $foto != 'nopic.png') {
  // remove the old file 
  $elim = unlink($_SERVER['DOCUMENT_ROOT'] . "fotoperfil/$foto");
}
// move the new file *after* the unlink
move_uploaded_file($_FILES["file"]["tmp_name"],"fotoperfil/" . $_FILES["file"]["name"]);
// If I understand your comment: update the $foto value with new filename
$foto = $_FILES["file"]["name"];
mysql_query("UPDATE miembros SET pic1 = '$foto' WHERE user = '$login_session'");