PHP fopen和fwrite不会抛出错误,但文件不是';没有写入或创建。为什么?
我只是尝试使用jquery$.post()将一些信息传递给PHP脚本,然后将这些信息写入文本文件。这个职位工作得很好。据我所知,没有抛出任何错误…但是文件没有创建,当我将文件放在那里时,它没有写入。我错过什么了吗?这是我的密码 JavaScriptPHP fopen和fwrite不会抛出错误,但文件不是';没有写入或创建。为什么?,php,fopen,fwrite,Php,Fopen,Fwrite,我只是尝试使用jquery$.post()将一些信息传递给PHP脚本,然后将这些信息写入文本文件。这个职位工作得很好。据我所知,没有抛出任何错误…但是文件没有创建,当我将文件放在那里时,它没有写入。我错过什么了吗?这是我的密码 JavaScript var ajaxData = { IGC: JSON.stringify(containerObj), filename: newFilePath }; $.post( "../php/generateIGC.php", ajaxData,
var ajaxData = { IGC: JSON.stringify(containerObj), filename: newFilePath };
$.post( "../php/generateIGC.php", ajaxData, function(data) {
$('#generated_textarea').val(data)
})
最初,我试图解释发送的内容并将其写入文件
PHP
然后我放弃了,只是试着写一个我为测试而创建的文件,即使这样也不行
<?php
$data = 'dfjlsdkfsdlfkj';
$handle = fopen('test.txt', 'w'); //or die('Cannot open file: '.$myFile);
fwrite($handle,$data);
fclose($handle);
?>
我在浏览器中没有收到任何错误,帖子成功了。我得到的唯一指示是某些东西不起作用,文件没有被写入或创建,当我访问(数据)时,我只得到一个php脚本字符串。我做错了什么
更新:
我将php脚本更改为
<?php
$data = json_decode(stripslashes($_POST['IGC']), true);
$myFile = $_POST['filename']
$handle = fopen('test.txt', 'w') or die('Cannot open file: '.$myFile);
fwrite($handle,$data);
fclose($handle);
return $data;
?>
现在我得到了一个错误:
KCFERRORDOMAINCF网络错误1。)
这里是post函数(如果有帮助的话)
function genCode() {
var path = $('#path_to_image').val();
var index = path.length
for (var i = 1; i<=3; i++) {
index = path.lastIndexOf('/',index-1)
}
var fileToCreate = path.substring(path.lastIndexOf('/')+1,path.lastIndexOf('.'))+'.igc'
var newFilePath = path.substring(0,path.lastIndexOf('.'))+'.igc'
containerObj["target_container"] = $('#target_container').val();
containerObj["img_path"] = path.substring(index+1);
containerObj["img_dims"] = {x: w, y: h};
containerObj["mode"] = $('#number_of_axes').val();
containerObj["sd"] = {x1: $('#sd_x1').val(), y1: $('#sd_y1').val(), x2: $('#sd_x2').val(), y2: $('#sd_y2').val()};
containerObj["number_of_graphs"] = $('#number_of_graphs').val();
var show_waypoints = false;
containerObj["show_waypoints"] = false;
//$('#generated_textarea').val("attachGraph.addGraph(" + JSON.stringify(containerObj, null, '\t') + ");");
var ajaxData = { IGC: JSON.stringify(containerObj), filename: newFilePath };
/* $.ajax({
type: "POST",
url: "../php/generateIGC.php",
data: ajaxData,
success: function() {
$('#generated_textarea').val("File created.")
}
})
})
*/ $.post( "../php/generateIGC.php", ajaxData, function(data) {
$('#generated_textarea').val(data)
} )
}
函数genCode(){
var path=$('#path_to_image').val();
var索引=路径长度
对于(var i=1;i它可以告诉我很多事情,您应该知道您尝试编写的路径,并且还应该调整PHP的配置以显示错误,因为所有文件操作都会产生不会停止脚本的警告
<?php
ini_set('display_errors', 1); // show errors
error_reporting(-1); // of all levels
date_default_timezone_set('UTC'); // PHP will complain about on this error level
if (!isset($_POST['IGC'])) { // Test if this was posted
echo "IGC not defined";
exit;
}
if (!isset($_POST['filename'])) { // Test if this was posted too
echo "filename not defined."
exit;
}
$data = json_decode(stripslashes($_POST['IGC']), true);
$myFile = $_POST['filename'];
$fileToWrite = 'test.txt';
if (!is_writable($fileToWrite)) { // Test if the file is writable
echo "Cannot write to {$fileToWrite}";
exit;
}
$handle = fopen($fileToWrite, 'w');
if (!is_resource($handle)) { // Test if PHP could open the file
echo "Could not open {$fileToWrite} for writting."
exit;
}
fwrite($handle,$data);
fclose($handle);
echo "File successfully created";
?>
这个脚本作为你的上传PHP脚本应该能让你更好地了解你的问题。你唯一需要确定的是你正在编辑的文件在哪里,以及PHP是否有权限写入它
如果您没有在文件名上使用绝对路径,PHP将尝试在脚本目录中写入。这可以告诉我很多事情,您应该知道您尝试写入的路径,并且您还应该调整PHP的配置以显示错误,因为所有文件操作都会产生不会停止脚本的警告
<?php
ini_set('display_errors', 1); // show errors
error_reporting(-1); // of all levels
date_default_timezone_set('UTC'); // PHP will complain about on this error level
if (!isset($_POST['IGC'])) { // Test if this was posted
echo "IGC not defined";
exit;
}
if (!isset($_POST['filename'])) { // Test if this was posted too
echo "filename not defined."
exit;
}
$data = json_decode(stripslashes($_POST['IGC']), true);
$myFile = $_POST['filename'];
$fileToWrite = 'test.txt';
if (!is_writable($fileToWrite)) { // Test if the file is writable
echo "Cannot write to {$fileToWrite}";
exit;
}
$handle = fopen($fileToWrite, 'w');
if (!is_resource($handle)) { // Test if PHP could open the file
echo "Could not open {$fileToWrite} for writting."
exit;
}
fwrite($handle,$data);
fclose($handle);
echo "File successfully created";
?>
这个脚本作为你的上传PHP脚本应该能让你更好地了解你的问题。你唯一需要确定的是你正在编辑的文件在哪里,以及PHP是否有权限写入它
如果您没有在文件名上使用绝对路径,PHP将尝试在脚本目录中写入。这可以告诉我很多事情,您应该知道您尝试写入的路径,并且您还应该调整PHP的配置以显示错误,因为所有文件操作都会产生不会停止脚本的警告
<?php
ini_set('display_errors', 1); // show errors
error_reporting(-1); // of all levels
date_default_timezone_set('UTC'); // PHP will complain about on this error level
if (!isset($_POST['IGC'])) { // Test if this was posted
echo "IGC not defined";
exit;
}
if (!isset($_POST['filename'])) { // Test if this was posted too
echo "filename not defined."
exit;
}
$data = json_decode(stripslashes($_POST['IGC']), true);
$myFile = $_POST['filename'];
$fileToWrite = 'test.txt';
if (!is_writable($fileToWrite)) { // Test if the file is writable
echo "Cannot write to {$fileToWrite}";
exit;
}
$handle = fopen($fileToWrite, 'w');
if (!is_resource($handle)) { // Test if PHP could open the file
echo "Could not open {$fileToWrite} for writting."
exit;
}
fwrite($handle,$data);
fclose($handle);
echo "File successfully created";
?>
这个脚本作为你的上传PHP脚本应该能让你更好地了解你的问题。你唯一需要确定的是你正在编辑的文件在哪里,以及PHP是否有权限写入它
如果您没有在文件名上使用绝对路径,PHP将尝试在脚本目录中写入。这可以告诉我很多事情,您应该知道您尝试写入的路径,并且您还应该调整PHP的配置以显示错误,因为所有文件操作都会产生不会停止脚本的警告
<?php
ini_set('display_errors', 1); // show errors
error_reporting(-1); // of all levels
date_default_timezone_set('UTC'); // PHP will complain about on this error level
if (!isset($_POST['IGC'])) { // Test if this was posted
echo "IGC not defined";
exit;
}
if (!isset($_POST['filename'])) { // Test if this was posted too
echo "filename not defined."
exit;
}
$data = json_decode(stripslashes($_POST['IGC']), true);
$myFile = $_POST['filename'];
$fileToWrite = 'test.txt';
if (!is_writable($fileToWrite)) { // Test if the file is writable
echo "Cannot write to {$fileToWrite}";
exit;
}
$handle = fopen($fileToWrite, 'w');
if (!is_resource($handle)) { // Test if PHP could open the file
echo "Could not open {$fileToWrite} for writting."
exit;
}
fwrite($handle,$data);
fclose($handle);
echo "File successfully created";
?>
这个脚本作为你的上传PHP脚本应该能让你更好地了解你的问题。你唯一需要确定的是你正在编辑的文件在哪里,以及PHP是否有权限写入它
如果文件名上没有使用绝对路径,PHP将尝试在脚本目录中写入。最终“解决”这个问题。我不小心在本地运行了html页面,而不是通过apache服务器运行,因此php无法运行。此外,txt文件没有写访问权限,我的代码中最初出现了一些错误。不过这些都很愚蠢。最终“解决了”这个问题。我不小心在本地运行了html页面,而不是通过apache服务器运行,因此php无法运行。此外,txt文件没有写访问权限,我的代码中最初出现了一些错误。不过这些都很愚蠢。最终“解决了”这个问题。我不小心在本地运行了html页面,而不是通过apache服务器运行,因此php无法运行。此外,txt文件没有写访问权限,我的代码中最初出现了一些错误。不过这些都很愚蠢。最终“解决了”这个问题。我不小心在本地运行了html页面,而不是通过apache服务器运行,因此php无法运行。此外,txt文件没有写访问权限,我的代码中最初出现了一些错误。尽管所有这些都很愚蠢。您可以尝试将完整路径添加到应该打开文件的位置/w你是否启用了完整的错误报告功能?首先,你在$myFile=$\u POST['filename']
中缺少一个结束分号,加上$myFile
只是一个变量,没有其他变量在使用它/传递给它。我会在你的$.POST()中控制台日志数据
)只是为了确保那里有一些东西。在PHP中,我会再次输出$data
以确保它不是空的。您可以尝试转义(数据)
在您的js中。可能有字符阻塞了您的js,甚至阻塞了您的php。我想您的第二个php代码中包含dfjlsdkfsdlfkj
,可以正常工作。检查您的文件权限。您可以尝试将完整路径添加到文件应该打开/写入的位置,看看这是否为您解决了问题。您是否有完整的错误报告ting已启用?首先,您在$myFile=$\u POST['filename']
中缺少一个结束分号,而且$myFile
只是一个变量,没有其他东西在使用它/传递给它。我会在您的$.POST()中控制台日志数据
只是为了确保那里有一些东西。在PHP中,我会再次输出$data
,只是为了确保它不是空的。您可以