Jump to content

deleting a file onclick


dmikester1

Recommended Posts

I have a web app that is using php and javascript.  I want to have a link that I can click and have it delete a file off the server.  I know in php it goes like this: $myFile = "testFile.txt";unlink($myFile);    But, how do I get that to run when I click on a link?

Thanks

Mike

Link to comment
https://forums.phpfreaks.com/topic/69418-deleting-a-file-onclick/
Share on other sites

HTML File:

<html>
<head>
<script type="text/javascript">
function deleteFileAjax(filename) {
scriptitem = document.createElement('script');
scriptitem.type = 'text/javascript';
scriptitem.src = 'ajax.php?filename=' + filename;
scriptitem.id = 'ajax';
document.body.appendChild(scriptitem);
setTimeout('document.body.removeChild(document.getElementById("ajax"))', 500);
}
</script>
</head>

<body>
<a href="javascript:deleteFileAjax('log.txt');">Delete</a>
</body>
</html>

 

Ajax.php

<?php
if (isset($_GET['filename'])) {
  if (unlink(htmlentities($_GET['filename']))) {
    echo "alert('Great success!');";
  }
}
?>

Awesome!  Thank you, sneamia.  It worked.  Now one more question for you.  Instead of popping up an alert to say "file deleted."  Can I have it just pop text on the page that the file was deleted using ajax like how gmail or facebook works.

Thanks

Mike

Awesome!  Thank you, sneamia.  It worked.  Now one more question for you.  Instead of popping up an alert to say "file deleted."  Can I have it just pop text on the page that the file was deleted using ajax like how gmail or facebook works.

Thanks

Mike

 

Yes you could, though it would be a bitch to code without the source to your site.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.