php_novice2007 Posted July 26, 2007 Share Posted July 26, 2007 Hi, I want my page to delete some files when it is unloaded. Can this be done with javascript? If someone can point me to a tutorial to do this it would be great! Thanks Quote Link to comment Share on other sites More sharing options...
php_novice2007 Posted July 26, 2007 Author Share Posted July 26, 2007 I just tried doing: function deleteFile() { var myActiveXObject = new ActiveXObject("Scripting.FileSystemObject"); var file = myActiveXObject.GetFile("<? echo $xmlName; ?>"); file.Delete(); } and it doesn't recognise what an ActiveXObject is... Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted July 26, 2007 Share Posted July 26, 2007 You can't really delete a file using javascript unless your using serverside asp javascipt. what you can do is use javascript to call a php page that deletes the file Quote Link to comment Share on other sites More sharing options...
php_novice2007 Posted July 26, 2007 Author Share Posted July 26, 2007 cool thanks Quote Link to comment Share on other sites More sharing options...
php_novice2007 Posted July 26, 2007 Author Share Posted July 26, 2007 hmm ok so I use window.location="deleteFiles.php"; to get to the page which would delete files, how do I pass the file name accross as a hidden variable? in javascript.. Thanks Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted July 27, 2007 Share Posted July 27, 2007 like this without javascript really <a href="deleteFiles.php?imageUrl=myJPG.jpg">delete image</a> or with javascript window.location="deleteFiles.php?imageUrl=myJPG.jpg"; then on your deleteFiles.php you just use a get <?php $_GET['imageUrl']; ?> Quote Link to comment Share on other sites More sharing options...
php_novice2007 Posted August 4, 2007 Author Share Posted August 4, 2007 Hi, So far I've got something like this: function doClose() { alert("here!"); GUnload(); window.location="deleteCreatedFiles.php?fname=<? echo $blah; ?>"; } <body onunload="doClose();"> And deleteCreatedFiles.php: session_start(); if(session_is_registered('userid')){ if (isset($_GET['fname'])) { try { $myFile = $_GET['fname'].".xml"; unlink($myFile); $myFile = $_GET['fname']."velData.xml"; unlink($myFile); if ($_SESSION['group']=="admin") { $URL="displayAdmin.php"; header ("Location: $URL"); } else { $URL="display.php"; header ("Location: $URL"); } } catch (Exception $e) { echo "Unable to delete file. <br>"; if ($_SESSION['group']=="admin") { echo "<a href=MenuAdmin.php>Back to Menu</a>"; } else { echo "<a href=Menu.php>Back to Menu</a>"; } } } else { echo "This page require input from somewhere else.<br>"; if ($_SESSION['group'] == "admin") { echo "<a href=MenuAdmin.php>Back to Menu</a>"; } else { echo "<a href=Menu.php>Back to Menu</a>"; } } } else { echo "You are not logged in."; } ?> I've found that this code work if the user clicks on the Back button of the browser, it also works for the back button that I made on the page (these either goes back to displayAdmin.php or display.php) I guess it works because of the code I've got in deleteCreatedFiles.php after the unlink() lines. But I want to be able to delete the files when the user clicks the refresh button as well as when the user closes the window. I know that doClose() is being called when the user clicks refresh since the alert poped up, but I'm not sure how to modify deleteCreatedFiles.php. The alert does not pop up when I click on the close window cross... how can I achieve the file deletion..? Thanks~! Quote Link to comment Share on other sites More sharing options...
php_tom Posted August 7, 2007 Share Posted August 7, 2007 Hey, Maybe this might help. On the page where you want to delete the files, add the following somewhere in the page (doesn't matter where): <img src='delete.php' width='0' height='0' style='visibility:hidden;' /> Then substitute whatever your deletion script is called for 'delete.php'. Not sure if this is what you're looking for... hope it helps... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.