使用取消链接使用AJAX和PHP删除图像时出错

时间:2012-09-12 18:20:03

标签: php ajax

我正在尝试使用AJAX和PHP从文件夹中删除所选图像。我没有看到任何错误,请您告诉我您对我的代码的看法? 提前致谢

AJAX代码:

function createAjax()
{
    var objAjax = false;

    if (window.XMLHttpRequest)
    {
        objAjax = new XMLHttpRequest ();
    }
    else
    {
        if (window.ActiveXObject)
        {
            try
            {
                objAjax = new ActiveXObject ("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                try
                {
                    objAjax = new ActiveXObject ("Microsoft.XMLHTTP");
                }
                catch (e)
                {
                }
            }
        }
        else
        {
            objAjax = false;
        }
    }
    return objAjax;
}

function eliminar(id_foto)
{
    var ajax = createAjax();
    ajax.open("POST", "delete_img.php",true);

    ajax.onreadystatechange=function()
    {
        if (ajax.readyState == 4)
        {
              //AQUI DEBES DE PONER EL CODIGO RESPECTIVO PARA ELIMINAR DEL NAVEGADOR
              // EL DIV EN CUESTION o simplemente hacer su contenido vacio, que es lo que hare
             document.getElementById("delete"+id_foto).innerHTML = "";
             document.getElementById("div_mensajes").innerHTML
        }
        else
        {
            document.getElementById("div_mensajes").innerHTML = "<br><center>Eliminando<img src = 'images/ajax-loader.gif' /></center>";
        }
    }

    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("id_foto="+id_foto);
}

HTML和PHP代码首先显示它:

$handle = opendir(dirname(realpath(__FILE__)).'/uploads/');
while($file = readdir($handle)) {

if($file !== '.' && $file !== '..') {
    if(file_exists('uploads/Thumbs.db')){
        unlink('uploads/Thumbs.db');
    }
    echo'<div class="gallery-item" id="delete'.$file.'">
          <p class="gallery-clean">
           <a class="image" rel="'.$file.'" rev="'.$file.'" href="uploads/'.$file.'" title="">
           <img src="uploads/'.$file.'" alt="'.$file.'"></a></p>
         <div>
          <a class="ico ico-delete" rel="9" rev="2" href="#" onclick = "eliminar_ajax('.$file.');"><span></span></a>
          <a class="ico ico-edit" rel="9" href="#"><span></span></a>
          <a class="ico ico-resize" rel="9" href="#"><span></span></a>
          <a class="ico ico-full" rel="group" href="#"><span></span></a>
         </div></div>';
    }
}

删除文件的PHP代码:

$dir = "uploads/";
$file = $_POST['id_foto'];
$img = $dir.$file;
unlink($img);

确定!我用这个解决了:

         script type="text/javascript">
          function deleteFile(fname,directory)
          {
        $.ajax({ url: "delete_img.php",
        data: {"file":fname,"directory":directory},
        type: 'post',
        success: function(output) {
        alert(output);
       $("#delete"+file).remove();
             }
          });
        }
        </script>

如果我调用它,我该如何删除div

        #delete.'<?php echo $file?>

2 个答案:

答案 0 :(得分:0)

你的形象的延伸是什么?像$file

一样将其添加到$file .= ".whateverextensionithas";

更清楚

$dir = "uploads/";
$file = $_POST['id_foto'];
$file .= ".whateverextensionithas";
$img  = $dir.$file;
unlink($img);

或者说真的很清楚

$dir = "uploads/";
$ext = ".whateverextensionithas";
$file = $dir.$_POST['id_foto'].$ext;
if(file_exists($file)) 
  unlink($file);
else
  echo "file does not exist";

答案 1 :(得分:0)

试试这段代码:

if(isset($_POST['id_foto'])){
    $dir = "uploads/";
    $file = $_POST['id_foto'];
    $img = $dir.$file;

    $f = fopen($dir."post.txt", 'w+');
    fwrite($f, $img);
    fclose($f);
}

并检查post.txt以查看您获得的输出类型。