0

我正在尝试使用 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?>
4

2 回答 2

0

你的形象的延伸是什么?添加到$filelike so$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";
于 2012-09-12T18:30:53.327 回答
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你得到什么样的输出。

于 2012-09-12T18:44:11.970 回答