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)
} )
}
full path
to where the file should be opened/written and see if that resolves it for you.$myFile = $_POST['filename']
- plus$myFile
is just a variable, nothing else is using it/passed to it.data
in your $.post() just to make sure there's something there. and again in your PHP i would output$data
just to make sure it isn't empty. you might tryescape(data)
in your js. there may be characters that are choking your js or even you php i supposedfjlsdkfsdlfkj
inside it, works. Check your file permissions.