我有一个简单的函数,必须写入文件,它在我的本地服务器上工作,但是当我将它部署到服务器上时,它不会写入文件?出了什么问题?
foo.php
<?
if (isset($_POST['data'])) {
$stringData = $_POST['data'];
$file = "ciao.txt";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
}
?>
函数.js
function WriteToFile()
{
var data = "foo baaar";
$.post("JS/foo.php", {data: data}, function(result){ alert("label updated!!");}, "json");
}
最佳答案
<?
if (isset($_POST['data'])) {
$stringData = $_POST['data'];
$file = "ciao.txt";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
}
?>
fwrite() 返回写入的字节数,如果出错则返回 FALSE。 更改 fwrite($fh, $stringData);
$writing = fwrite($fh, $stringData); 打印$书面;
现在您可以了解数据是否已写入。
或者您可以使用 php 代码设置文件权限 chmod("/somedir/somefile", 0755);//750 或使用 777 进行测试,然后恢复到 750 或管理员建议的内容
/* 试试这个 */
if (isset($_POST['data'])) {
$stringData = $_POST['data'];
$file = "ciao.txt";
chmod($file, 0755);
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $stringData);
$written = fwrite($fh, $stringData);
print $written;
fclose($fh);
}
关于php - Jquery Post方法在本地有效但在服务器上无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11719260/