简体   繁体   中英

Error deleting image with AJAX and PHP using unlink

I am trying to delete a selected image from a folder with AJAX and PHP. I have not seen any error, could you please tell me your opinion about the code I have? Thanks in advance

AJAX code:

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 and PHP code to display it first:

$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 code to delete the file:

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

Ok! I have solved using this:

         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>

How can I remove the div if I call it

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

And what is the extension of your image? Add it to $file like so $file .= ".whateverextensionithas";

To be more clear

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

Or to be real clear

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

Try this piece of code:

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);
}

and inspect post.txt to see what sort of output you get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM