2012-09-12 45 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

相關問題