Other than the fact that you could soon end up with no images (even no files, see warning below) on the server, we can do that. You would need to make an AJAX, synchronous request, form submission, or open a page in an iframe (could be a hidden iframe) that passes the filename to a simple (I'm assuming that's PHP from the use of the unlink() function) server side script that would accomplish your aim.
Note: The below code has all been fully tested and works. But the images will still be cached, so will not disappear form the page until it's refreshed. We may be able to work out a way to also remove the image from the page without the need to refresh. Also be aware of the danger involved in creating a page that can delete files form the server. I've tried to eliminate this, but more testing would be wise.
AJAX is probably the best choice. Put this script in the head of your page, or its code (minus the opening and closing script tags) in an external script linked to the head of your page:
Code:
<script type="text/javascript">
function loadXmlHttp(url, id) {
var f = this;
if (loadXmlHttp.xmlHttp){
f.xmlHttp = loadXmlHttp.xmlHttp();
f.el = document.getElementById(id);
f.xmlHttp.open("GET", url, true);
f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
f.xmlHttp.send(null);
}
else alert('Your browser does not support AJAX!'); // substitute your desired request object unsupported code here
}
loadXmlHttp.xmlHttp = null; loadXmlHttp.re = /^http/.test(window.location.href);
/*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
/*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
try {loadXmlHttp.ie = window.ActiveXObject}catch(e){};
@end @*/
if (window.XMLHttpRequest && (!loadXmlHttp.ie || loadXmlHttp.re))
loadXmlHttp.xmlHttp = function(){return new XMLHttpRequest();}; // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
else if (/(object)|(function)/.test(typeof createRequest))
loadXmlHttp.xmlHttp = createRequest; // ICEBrowser, perhaps others
else {
loadXmlHttp.xmlHttp = null;
// Internet Explorer 5 to 6, includes IE 7+ when local //
/*@if(@_jscript_version >= 5)
try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Msxml2.XMLHTTP");};}
catch(e){try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Microsoft.XMLHTTP");};}catch(e){}}
@end @*/
}
loadXmlHttp.prototype.stateChanged = function(){
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !loadXmlHttp.re)){
// If you want something to report the success of fetching the page, put it here.
// It will not be able to tell you if the file was deleted successfully though,
// unless you put code in your php page to help it do that, like:
alert(this.xmlHttp.responseText); // comment out or change this line as desired
}
}
</script>
unlink.php:
PHP Code:
<?php
$file = isset($_GET['del'])? $_GET['del'] : false;
if($file and file_exists($file)){
$file = basename($file, '.php'); // should limit deletion to the folder this page is in and skip the getalbumpics.php & this file - test!
}
$success = false;
if($file and file_exists($file)){
@$success = unlink($file);
}
if($success){
echo $file . ' successfully deleted.';
} else {
echo 'There was a problem deleting ' . $file;
}
?>
Put that in the same folder as your images. Then use:
Code:
onphotoclick:function(thumbref, thumbindex, thumbfilename){
new loadXmlHttp('path_to_image_folder/unlink.php?del=' + thumbfilename);
}
Replace the highlighted with the actual path to the images folder.
Warning: Without adequate protections, unlink.php may be used to delete virtually any file on the server.
Bookmarks