2012-09-12 41 views
0

我想从包含AJAX和PHP的文件夹中删除选定的图像。我还没有看到任何错误,请告诉我你对我的代码的看法? 在此先感谢使用取消链接删除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?> 
+1

它在哪里失败? HTML是否按预期工作? Ajax是否按预期发送请求?你到达PHP吗?你可以抛弃'unlink()'的结果:'var_dump(unlink($ img));' – Tchoupi

+1

没有任何反对纯JavaScript的,但jQuery更容易使用(特别是在做AJAX调用时) – Chris

+0

什么是你的'存在'('Thumbs.db')'测试在while循环的顶部?对于该目录中的每个文件,都会反复测试和取消数据库文件 - 同一个文件的连接。你只是试图让**真的**确定它被删除?因为大多数文件在解除链接后不会自发跳回到存在状态。 –

回答

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"; 
+0

我试试看:$ img = $ dir。$ file。“jpg”;它可能是JavaScript的电话? – charlyta

+0

然后用'$ img = $ dir。$ file。'来更加努力。“.jpg”;' – dbf

+0

我没有结果。我认为我输了一些东西:onclick =“eliminar_ajax('。$ file。');” – charlyta

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看看你得到了什么样的输出。

+0

post.txt文件它是空的 – charlyta

+0

但存在?嗯... $ _POST ['id_foto']是submited,但是是空的:/ – Eugen

+0

你觉得这条记忆是错误的吗? onclick =“eliminar_ajax('。$ file。');”> – charlyta

相关问题