I am using UNLINK with PHP and AJAX . I know that this way is very dangerous, because everyone can delete any files. But I need to use AJAX because I cannot reload the page when deleting files.
So, how do I do to delete a file only for the user who owns it?
Please let me know other things if you think I'm doing something wrong here or something else that you mean and you think it will be useful :)
My PHP code is:
<?php $photo_id = $_GET['photo_id']; $thumbnail_id = $_GET['thumbnail_id']; function deletePhotos($id){ return unlink($id); } if(isset($photo_id)){ deletePhotos($photo_id); } if(isset($thumbnail_id)){ deletePhotos($thumbnail_id); } ?>
My AJAX Code:
function deletePhoto(photo, thumbnail){ var photos = encodeURIComponent(photo); var thumbnails = encodeURIComponent(thumbnail); if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("media").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET", "http://192.168.2.104/images/users/delete_photo.php?photo_id="+photos+"&thumbnail_id="+thumbnails, true); xmlhttp.send(); }
security ajax php delete-file unlink
Adam
source share