0

I am trying to send some text from my webpage to a webserver and save it within a file but I can't seem to get it working and I don't have any errors being produced to tell me why.

I'm trying to use JavaScript to send a post request to the server and then have the PHP process the request and save the contents into a file. The code I'm using is below:

JavaScript

function sendCode() {
    const copyCode = document.getElementById("terraCode").textContent;
    const textArea = document.createElement('textarea');
    textArea.textContent = copyCode; //The text being sent
    
    var code = new FormData();
    code.append("code", copyCode);
    var xhr = new XMLHttpRequest();
    xhr.open('post', './test.php', true); //Request and location of PHP
    xhr.send(code);
    
}

document.getElementById("TestCode").addEventListener("click", sendCode); //Execute function above based on button click

PHP

<?php
if (!empty($_POST['code'])) {
    $data = $_POST['code'];
    $fname = "test" . ".tf"; //generates file name

    $file = fopen("./file" . $fname, 'w'); //creates new file
    fwrite($file, $data);
    fclose($file);
}
?>

I am pretty confident it is getting the text correctly as it is present when I check inspect element on the page, but nothing seems to be happening server side and I'm not sure if the data is even reaching the server.

Can anyone explain why this wouldn't be working?

Edit: I'm running an Nginx web server and PHP7.4 is installed and enabled on the server

Luke
  • 33
  • 5
  • Have you checked your browser's Developer toolbar to see if the request is taking place (under the Network tab)? – El_Vanja Feb 05 '21 at 14:57
  • Yep. It's listed under the network tab with a 200 response and xhr type. Size is about 230bytes and response time is around 35ms – Luke Feb 05 '21 at 15:01
  • Alright. Now you can approach PHP debugging. For starters, dump `$data` and see what it contains. You can see the result when you click on "Response" once you select the request in the Network tab. You should also turn on [error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – El_Vanja Feb 05 '21 at 15:05
  • Ok so I've checked the response header and it is definitely running the php code as the response contains the echo text I put inside the PHP script. But it doesn't seem to be creating the file and putting the data that's being sent into it? – Luke Feb 06 '21 at 15:34
  • Have you tried to dump `$file`? Don't `echo` variables, use `var_dump` to get more info. – El_Vanja Feb 06 '21 at 16:05
  • The response for $file when I did a var_dump says "bool(false)". I am currently exploring if it's a permissions issue of the file not being able to write to the directory and from googling this seems to be a common issue – Luke Feb 06 '21 at 22:18
  • This question about getting a [detailed error](https://stackoverflow.com/questions/2470217/detailed-error-on-fopen) might be of help. – El_Vanja Feb 06 '21 at 23:16
  • Error is "failed to open stream: Permission denied" for the fopen line but the php file has full permissions and I've double checked the user that runs for PHP and it is also the owner of the file? – Luke Feb 06 '21 at 23:51

1 Answers1

0

I figured out the problem.

It was a permissions issue but it was to do with the user that was running the php service.

Once this was changed to the correct user the script ran as expected.

Thanks for the help.

Luke
  • 33
  • 5