0

I have a pre-registration data file ('tmp/data.txt') that I have to process 5 to 25 times a night at club meetings. Permissions are 644. I use an html web app to generate users' ID #'s.

sample: 10673062 12345678 etc.

After reading the file, it needs to be truncated to 0 bytes. I don't want to process the same ID #'s repeatedly.

I have tried every php trick in the book:

<script>

function remake() { 

// do various js operations, then:

<?php $fh = fopen( 'tmp/data.txt', 'w' ); fclose($fh);?>
<?php file_put_contents("tmp/data.txt", "");?>
<?php $fp = fopen("tmp/data.txt", "w+"); ftruncate($fp, 0); fclose($fp);?>

// do more js stuff.

}

</script>

But the file remains intact. What do I have to do (chmod or chgrp possibly ?) to make the file accessible for truncation by php?

SOLVED: made an external php 'eraseRegs.php;' called it and it worked. Thanks!

verlager
  • 794
  • 5
  • 25
  • 43
  • 1
    Sorry for the dumb question, but it's definitely supposed to be "tmp/data.txt", right? Not "/tmp/data.txt"? – Don't Panic Sep 09 '21 at 20:31
  • 4
    _Side note:_ Looking at your code, it seems like you're trying to do some js, then some php and then again some js. That's not how things work though. PHP is server side, which means it will be executed way before the the browser gets the result, which is where the JS (client side) gets executed. Conclusion, it won't be "do js, then php and then more js". It will be be: "Do all PHP, then return the JS to the browser that will execute the js": https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – M. Eriksson Sep 09 '21 at 20:32
  • Oh yeah, I was focused on the PHP aspect of it and totally missed that, @MagnusEriksson. My question is probably a moot point, in that case. – Don't Panic Sep 09 '21 at 20:33
  • Yes, the file location is "tmp/data.txt" – verlager Sep 09 '21 at 21:04
  • The order web-server processes work is: `PHP --> HTML --> JS` you are trying to do `HTML --> JS --> PHP` which will give unexpected results at best. Try creating a JS AJAX call to ask a PHP file to truncate the file (with security so only the valid file can be changed). – Martin Sep 09 '21 at 21:08

1 Answers1

0

In your case you have to use some ajax request to do an action in backend. The PHP code is not working in JavaScript functions. Using jQuery your script could look like this

<script>

    function remake() { 
       // do various js operations, then:

       $.ajax({
          url: 'truncate.php'
       }).done(function(){
          console.log('truncate complete');
       });
    }

    // do more js stuff.

</script>

or with pure JS

<script>

    function remake() { 
       // do various js operations, then:

       var request = new XMLHttpRequest();
       request.open('GET', 'truncate.php');
       request.send();      
    }

    // do more js stuff.

</script>

And in truncate.php file do anything you want with this data file.

zajonc
  • 1,935
  • 5
  • 20
  • 25
  • deleting and recreating files is computationally expensive compared to editing. – Martin Sep 09 '21 at 21:07
  • @Martin - you are right. I suggested this because the approach is easier and you know for sure that you have done it. However, I corrected my answer. – zajonc Sep 09 '21 at 21:09
  • @zajonic's answer bypasses the undesired php white screen and no page refresh is needed. Thank you! Perfect and complete. – verlager Sep 09 '21 at 22:12