Delete file onclick with PHP [closed]

风流意气都作罢 提交于 2019-12-10 15:49:52

问题


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?


回答1:


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__);
    }
?>



回答2:


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);



回答3:


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
}



回答4:


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.




回答5:


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>



回答6:


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



来源:https://stackoverflow.com/questions/13295307/delete-file-onclick-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!