tags:

views:

49

answers:

3

Hi,

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/yyy';
                unlink($file_to_delete);
                rmdir("xxx/yyy"); 
            }
        }   ?>

}

</script>

Thanks Jean

+2  A: 

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).

Jamiec
check question again, could you tell me how I could trigger the php on page unload? I am deleting a folder on the server not client
Jean
@Jean: Your JavaScript code runs on the client, therefore your code is attempting to delete a folder on the client machine.
Andy E
@andy The script runs on the client side, but the path need not be the same, it can be directed to the server too. Anyways, I want to delete the folder after the page unloads, or page is refreshed. How can I trigger the function for such and event?
Jean
A: 

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.

Amir Raminfar
Can you check the code, it is not on the client, but server, just forget where it is, could you provide where the mistake is in the code
Jean
uuuhh you are not understanding how php works. if you are expecting on page unload it will execute that php code then you are misunderstanding. php code gets executed once on page and not again via javascript. if you need to call some php function after the page is loaded then you need make an ajax call.
Amir Raminfar
@amir It deletes the folder, but not before the page unloads
Jean
A: 

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.

norwebian