admin管理员组文章数量:1313934
I'm trying to delete a file with ajax request:
javascript:
function deleteFile(file_path)
{
var r = confirm("Sure?")
if(r == true)
{
$.ajax({
url: 'delete_file.php',
data: {'file' : file_path },
method: 'GET',
success: function (response) {
alert('Deleted!');
},
error: function () {
alert('Not Deleted!');
}
});
}
}
delete_file.php :
unlink($_GET['file']);
It returns true on succes,but the file is not deleted.
I'm trying to delete a file with ajax request:
javascript:
function deleteFile(file_path)
{
var r = confirm("Sure?")
if(r == true)
{
$.ajax({
url: 'delete_file.php',
data: {'file' : file_path },
method: 'GET',
success: function (response) {
alert('Deleted!');
},
error: function () {
alert('Not Deleted!');
}
});
}
}
delete_file.php :
unlink($_GET['file']);
It returns true on succes,but the file is not deleted.
Share Improve this question asked Feb 4, 2015 at 8:58 Petru LebadaPetru Lebada 1,68211 gold badges47 silver badges67 bronze badges 6- does your php/server instance have read/write/modify permissions in the folder where you are trying to delete? – Tschallacka Commented Feb 4, 2015 at 9:00
- Check your link path before unlink. May be you link was relative not absolute – Nes Commented Feb 4, 2015 at 9:00
-
You should probably go into Debug mode and check the content of the
response
variable - or just point your web browser at the path (delete_file.php?file..
). Possbily PHP is giving you errors, such as permission denied, but still returning a200 OK
status code. – CompuChip Commented Feb 4, 2015 at 9:01 - I will check and return with a review – Petru Lebada Commented Feb 4, 2015 at 9:03
-
There's a HTTP verb called
DELETE
. If you <3 semantics, use that overGET
. – Jørgen R Commented Feb 4, 2015 at 9:03
2 Answers
Reset to default 5Check the response in AJAX, Best is use JSON DATA to check the response:
// Default AJAX request type is GET so no need to define
$.ajax({
url: 'delete_file.php',
data: {'file' : file_path },
dataType: 'json',
success: function (response) {
if( response.status === true ) {
alert('File Deleted!');
}
else alert('Something Went Wrong!');
}
});
Do It like this in PHP:
// First Check if file exists
$response = array('status'=>false);
if( file_exists('FILE_PATH/FILENAME') ) {
unlink('FILE_PATH/FILENAME');
$response['status'] = true;
}
// Send JSON Data to AJAX Request
echo json_encode($response);
Make sure you are giving the plete path with filename to unlink() function
Try this you need to check file, give permission, then delete it
$filename = 'full absolute file path';
if(file_exists($filename)) {
@chmod($filename, 0777);
@unlink($filename);
return true;
}
As there can be two issues either the file path is not correct or the file is not having permission.
With the above code both will be checked.
本文标签: javascriptDelete a file with ajax requestStack Overflow
版权声明:本文标题:javascript - Delete a file with ajax request - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741914374a2404637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论