Search code examples
javascriptphpjqueryajaxcloud9-ide

Make new file in PHP on Cloud9IDE


I'm using Cloud9IDE on my Chromebook and am trying to write a new file in PHP.

Basically when I click a button I use JQuery's $.ajax to read my php file and make the hello.txt file from the php script. I have an alert setup so it is calling the function and reading the php file, but my new file is not being made.

My question is why isn't this working, and how can I fix this

index.js

$("[data-action=writefile]").click(function(){
  $.ajax({
    url: 'save.php',
    success: function(msg) {
      alert('file saved: \n' + msg);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
      alert("Some error occured");
    }
  });
});

save.php

<?php
  $filename = 'hello.txt';
  @ $fp = fopen("exports/".$filename, 'wb');
  if (!$fp) {
    echo '<p><strong>Cannot generate message file</strong></p></body></html>';
    exit;
  } else {
    $outputstring  = 'Hello, i have been generated by the button';
    fwrite($fp, $outputstring);
    Echo "Message inserted";
  }
?>

index.html

<button class="btn--default" data-action="writefile">Write to File</button>

Solution

  • try opening your cloud9 terminal and change the permission:

    $ cd /exports
    

    change the user permission using chmod.

    $ chmod 777 -R ./