Delete folder when document is refreshed or unloaded using javascript
I want to delete a folder when the page is refreshed on closed. Here is my code, what exactly is wrong, why is it not deleting the directory
window.onunload=closed;
function closed()
var FolderName = "uploads-temp/"+<? echo $create_temp_dir; ?>+"*";
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.DeleteFolder("xxx/yyy*", true);
}
[edit] --------------------------using php----------------
<script language="javascript" type="text/javascript">
window.onunload=closed;
function closed() {
<?
$d = opendir("xxx/yyy");
while (($file = readdir($d)) !== false) {
if (($file != ".") && ($file != "..")){
$file_to_delete = 'xxx/yy开发者_运维技巧y';
unlink($file_to_delete);
rmdir("xxx/yyy");
}
} ?>
}
</script>
Thanks Jean
1) ActiveXObject will only work in IE
2) You are trying to delete a directory on the client machine (was this your intention?)
3) Deleteing a directory on the client machine will be subject to many security restrictions (it'll fail almost unanimously in the Internet zone, you might have slightly more luck in Intranet zone).
You can't delete anything using javascript which is only executed on the browser.
You also can't delete anything on the user's computer using php or any other language. Deleting something in php will delete it on the server.
The only way I can think of is using an java applet but even that would require so many security restrictions.
You'll need to trigger a server-side script which does what you want, something like your php closed()-function. If you put that logic into a php-file called "deleteFolder.php", and set up an ajax-call (search for xmlhttp.open
, or use a framework like jQuery) for this file at the golden moment client-side (you'll have to do a little research if you need to handle feedback from the script), you'll be on your way.
精彩评论