[英]Error deleting image with AJAX and PHP using unlink
I am trying to delete a selected image from a folder with AJAX and PHP. 我正在尝试使用AJAX和PHP从文件夹中删除选定的图像。 I have not seen any error, could you please tell me your opinion about the code I have?
我没有看到任何错误,请问您对我拥有的代码有何看法? Thanks in advance
提前致谢
AJAX code: 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 and PHP code to display it first: 首先显示它的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 code to delete the file: PHP代码删除文件:
$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 如果我叫它怎么去掉div
#delete.'<?php echo $file?>
And what is the extension of your image? 图像的扩展名是什么? Add it to
$file
like so $file .= ".whateverextensionithas";
像这样将其添加到
$file
$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. 并检查
post.txt
以查看获得的输出类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.