我正在尝试使用jquery、ajax和PHP向文本文件添加字符。POST ajax调用返回成功,但PHP函数未执行。有人看到我的错误了吗?变通办法?
<html>
<head>
<title>AjaxSend</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script>
$(function() {
$(".update_button").click(function() {
{
$.ajax({
type: "POST",
url: "changetext.php",
cache: false,
success: function(html){
alert('You were successful');
}
});
} return false;
});
});
</script>
</head>
<body>
<button class="update_button">Put File</button>
</body>
</html>
PHP:
<?php
file_put_contents('test.txt','Text to be added');
?>
1。检查文件是否存在,但没有更新
尝试将FILE_APPEND
作为第三个参数应用于file_put_contents()
2.检查您的权限
确保你在写test.txt
的目录中有写访问权限。考虑到你所展示的代码,这很可能是你的PHP脚本存储的根目录。
3.更新权限并选择要写入的目录
创建一个目录(就在那里存储脚本changetext.php
)。给它任何名称,例如:output
然后将目录的权限设置为777,并将changetext.php
更改为:
file_put_contents('output/test.txt','Text to be added', FILE_APPEND);