使用 PHP 删除文件 onclick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13295307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete file onclick with PHP
提问by Code Lover
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?
我究竟做错了什么?
回答by Jocelyn
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:
如果您更喜欢使用 POST 而不是 GET 方法,请使用以下代码:
<form method="post">
<input name="delete" type="submit" value="Delete Now!">
</form>
<?php
if(isset($_POST['delete']))
{
unlink(__FILE__);
}
?>
回答by eusoj
You need to load this action via Javascript. If you're using jQuery you can't try something like that
您需要通过 Javascript 加载此操作。如果你使用 jQuery 你不能尝试这样的东西
Your Javascript
你的 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.
你的 deletefile.php 看起来像那样。
<?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);
回答by Josh
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.
您不了解客户端和服务器代码之间的分离。Javascript 不能像那样调用 PHP。PHP 将在页面在服务器上构建时立即运行,而不是存储以供以后使用。
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.
你需要发出一个 AJAX 请求来删除 onClick,或者创建一个像 /delete/$ID/ 这样的新页面来为你删除,或者,就像 Jocelyn 刚刚击败我一样,如果GET / POST 参数已设置。
Although, it's worth noting that __FILE__is the file that that code is in, so, it's going to kill itself.
不过,值得注意的是,这__FILE__是该代码所在的文件,因此,它会杀死自己。
回答by alloyking
Is this PHP? You can't run a PHP function from inside javascript. Instead you need to load/redirect/post to a php file.
这是 PHP 吗?您不能从 javascript 内部运行 PHP 函数。相反,您需要加载/重定向/发布到 php 文件。
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
}
回答by Darius
You cannot run a PHP function as an onclick, javascript event. You need to run that function in a fashion like so:
您不能将 PHP 函数作为 onclick、javascript 事件运行。您需要以如下方式运行该函数:
<?php
if (isset($_GET['delete'])) {
unlink($_GET['delete']);
}
?>
<html>
<a href="?delete=/PATH/TO/FILE">Delete Now</a>
</html>
回答by Jurudocs
You need a post request that php processes...or an ajax/javascript function that runs a php script...
您需要一个 php 处理的 post 请求……或者一个运行 php 脚本的 ajax/javascript 函数……
Here is an example with ajax http://www.website-php.com/de/tutorials/treeview/treeview-04.html
这是一个 ajax 示例http://www.website-php.com/de/tutorials/treeview/treeview-04.html

