I want to delete a file when a user clicks on a delete link. But when I go to my page, the file gets deleted and I don't know why:
echo '<h3><a onclick="'.unlink(__FILE__).'">Delete Now!</a></h3>';
What am I doing wrong?
I want to delete a file when a user clicks on a delete link. But when I go to my page, the file gets deleted and I don't know why:
echo '<h3><a onclick="'.unlink(__FILE__).'">Delete Now!</a></h3>';
What am I doing wrong?
This code will delete the current file when the user clicks the link:
<h3><a href="?delete=1">Delete Now!</a></h3>
<?php
if(isset($_GET['delete']))
{
unlink(__FILE__);
}
?>
If you prefer to use POST instead of GET method, use this code:
<form method="post">
<input name="delete" type="submit" value="Delete Now!">
</form>
<?php
if(isset($_POST['delete']))
{
unlink(__FILE__);
}
?>
You need to load this action via Javascript. If you're using jQuery you can't try something like that
Your Javascript
<script type="text/javascript">
$('.delete').live('click',function(){
deleteFile( $(this).attr('id') );
});
function deleteFile(id){
$.ajax({
url: 'deletefile.php?fileid='+id,
success: function() {
alert('File deleted.');
}
});
}
</script>
Your deletefile.php look like that.
<?php
$fileid = $_GET['fileid'];
//HERE IS THE LOGIC TO FIND THE PATH OF YOUR FILE
unlink($file); //You can add more validations or full paths
?>
And your link must have the following structure
printf("<a id='%s' class='delete'>Delete</a>",$youridfile);
You're not understanding the separation between client and server code. Javascript can't just call PHP like that. The PHP will run immediately as the page is being built on the server, not stored for later use.
You'll need to make an AJAX request to delete onClick, or, make a new page like /delete/$ID/ that will delete for you, or, as Jocelyn just beat me to, make the same page able to delete if a GET / POST parameter is set.
Although, it's worth noting that __FILE__
is the file that that code is in, so, it's going to kill itself.
Is this PHP? You can't run a PHP function from inside javascript. Instead you need to load/redirect/post to a php file.
echo '<h3><a href="deleteScript.php" >Delete Now!</a></h3>';
edit:
function table_exists($tablename, $database = false) {
if(!$database) {
$res = mysql_query("SELECT DATABASE()");
$database = mysql_result($res, 0);
}
$res = mysql_query("
SELECT COUNT(*) AS count
FROM information_schema.tables
WHERE table_schema = '$database'
AND table_name = '$tablename'
");
return mysql_result($res, 0) == 1;
}
if(table_exists('my_table_name')) {
// do something
}
else {
// do something else
}
You cannot run a PHP function as an onclick, javascript event. You need to run that function in a fashion like so:
<?php
if (isset($_GET['delete'])) {
unlink($_GET['delete']);
}
?>
<html>
<a href="?delete=/PATH/TO/FILE">Delete Now</a>
</html>
You need a post request that php processes...or an ajax/javascript function that runs a php script...
Here is an example with ajax http://www.website-php.com/de/tutorials/treeview/treeview-04.html