I am simply trying to pass some information to a PHP script using jquery $.post() and then write that information to a text file. The post is working just fine. No errors are thrown, as far as I know...but the files aren't created and when I put the file there it isn't written to. Am I missing something? Here's my code.
JavaScript
var ajaxData = { IGC: JSON.stringify(containerObj), filename: newFilePath };
$.post( "../php/generateIGC.php", ajaxData, function(data) {
$('#generated_textarea').val(data)
})
Originally I tried to interpret what was being sent and write it to the file.
PHP
<?php
$data = json_decode(stripslashes($_POST['IGC']), true);
$myFile = $_POST['filename'];
$handle = fopen($myFile, 'w'); //or die('Cannot open file: '.$myFile);
fwrite($handle,$data);
fclose($handle);
echo "File successfully created";
?>
Then I gave up and just tried to write to a file that I created for testing, and even this won't work.
<?php
$data = 'dfjlsdkfsdlfkj';
$handle = fopen('test.txt', 'w'); //or die('Cannot open file: '.$myFile);
fwrite($handle,$data);
fclose($handle);
?>
I'm not getting any error in the browser, and the post succeeds. The only indication I have that something isn't working is the file not being written to or created, and when I access (data) I just get a string of the php script. What am I doing wrong here?
UPDATE:
I changed the php script to this
<?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;
?>
and now I get the error:
kCFErrorDomainCFNetwork error 1.)
Here is the post function if it helps
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)
} )
}
Finally "solved" this problem. I had been accidentally running the html page locally, and not through the apache server, so the php couldn't run. Additionally, the txt files didn't have write access and there were some errors in my code originally. All pretty silly though.