I have a javascript function below where it removes an appended file name from .listImage when the user clicks on the "Delete" button:
function stopImageUpload(success, imagefilename){
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
$('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage">Delete</button><br/><hr/></div>');
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
$(".deletefileimage").on("click", function(event) {
$(this).parent().remove();
});
return true;
}
But what I want to know is that when the user clicks on the delete button, I also want the file to be deleted from the server. How can this be done?
The folder the files are stored in is known as ImageFiles and the file name code on server side is $_FILES["fileImage"]["name"]
.
The uploading of files into the server is on a seperate page on a php script which is below:
<?php
$result = 0;
if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
$parts = explode(".",$_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while( file_exists("ImageFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileImage']['name'] = $base."_".$n.".".$ext;
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
}
else
{
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
}
?>
I have a javascript function below where it removes an appended file name from .listImage when the user clicks on the "Delete" button:
function stopImageUpload(success, imagefilename){
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
$('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage">Delete</button><br/><hr/></div>');
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
$(".deletefileimage").on("click", function(event) {
$(this).parent().remove();
});
return true;
}
But what I want to know is that when the user clicks on the delete button, I also want the file to be deleted from the server. How can this be done?
The folder the files are stored in is known as ImageFiles and the file name code on server side is $_FILES["fileImage"]["name"]
.
The uploading of files into the server is on a seperate page on a php script which is below:
<?php
$result = 0;
if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
$parts = explode(".",$_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while( file_exists("ImageFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileImage']['name'] = $base."_".$n.".".$ext;
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
}
else
{
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
}
?>
Share
Improve this question
edited May 1, 2012 at 13:23
user1364441
asked May 1, 2012 at 13:16
user1364441user1364441
851 silver badge8 bronze badges
2
- 1 Your code is prone to attacks. You should never store any server data like paths on the clients! You need to make a handler from where you ajax a unique id assigned only to that file. The handler then verifies the ID and will delete it on the server with out any client knowing any sort of paths or file names. Otherwise I can start deleting your whole system! – Piotr Kula Commented May 1, 2012 at 13:19
- @ppumkin my actual php script where it uploads files is on a seperate age from the javascript, is there a way deleting a file from the server by storing it in that page? – user1364441 Commented May 1, 2012 at 13:30
2 Answers
Reset to default 4Check out this - http://www.php/unlink
Try:
<?php
// Delete image from server
unlink($path_to_file);
?>
(The unlink ;))