Skip to content

Instantly share code, notes, and snippets.

@SoldierCorp
Created March 28, 2014 00:37
Show Gist options
  • Save SoldierCorp/9822494 to your computer and use it in GitHub Desktop.
Save SoldierCorp/9822494 to your computer and use it in GitHub Desktop.
Delete/remove file with PHP and jQuery Ajax
<!doctype html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Eliminar archivo con PHP y jQuery Ajax</title>
<!-- Bootstrap-->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<!-- jQuery-->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(document).on('ready', function() {
$('a').on('click', function(e) {
e.preventDefault();
$.ajax({
url: 'process.php',
type: 'post',
dataType: 'json'
})
.done(function() {
alert("Eliminado correctamente!");
})
.fail(function() {
alert("Ha ocurrido un error");
})
.always(function() {
});
})
})
</script>
<style>
body {
width: 30%;
margin: 30px auto;
}
</style>
</head>
<body>
<h1>Eliminar archivo con PHP y jQuery Ajax</h1>
<a herf="#" class="btn btn-primary">Eliminar archivo</a>
</body>
</html>
<?php
$file = 'imagen.png';
$json;
if (is_file($file)) {
chmod($file,0777);
if(!unlink($file)) {
echo false;
}
echo $json['success'] = true;
} else {
echo false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment