簡體   English   中英

使用unlink刪除帶有AJAX和PHP的圖像時出錯

[英]Error deleting image with AJAX and PHP using unlink

我正在嘗試使用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?>

圖像的擴展名是什么? 像這樣將其添加到$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";

嘗試這段代碼:

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以查看獲得的輸出類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM