3

I have a small PHP script being run when I press a button on some HTML page:

<?php
if(isset($_POST['field1'])) {
    $data = $_POST['field1'] . "\n";
    $ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

When I give the html some text and run the script I get an echo:

16 bytes written to file

However when I then go to open the file, it's empty, and reads as 'zero bytes' in size.

I've tried messing with permissions and checked the log files with no hints, I've made sure the cirrect text is getting to the php script. I'm totally stumped, any suggestions?

NNNNNNNNNNDelicious
  • 933
  • 3
  • 7
  • 19
  • How do you open the file? – Sven Jul 09 '15 at 22:55
  • $ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX); – NNNNNNNNNNDelicious Jul 09 '15 at 22:57
  • It seems to be opening the file.. because if I change opermissions or move the file I get an error – NNNNNNNNNNDelicious Jul 09 '15 at 22:57
  • I was referring to this comment: "However when I then go to open the file, it's empty, and reads as 'zero bytes' in size." How do you do this? Maybe your code to write the file isn't wrong, but to read it. – Sven Jul 09 '15 at 23:02
  • @Sven Im checking by eye in my file browser where the files located – NNNNNNNNNNDelicious Jul 09 '15 at 23:10
  • And which one is that? Sorry to sound picky, but I'm trying to eliminate side effects. – Sven Jul 09 '15 at 23:13
  • no problem! I'm looking at the /tmp/mydata.txt in the htdocs/tmp/ folder I made. The script is run out of htdocs and seems to be able to find it (it complains if I move it) so I think it knows its there just isnt writing for some reason. – NNNNNNNNNNDelicious Jul 09 '15 at 23:21
  • Now here is the point: You don't write to that file. You write to `/tmp/mydata.txt`, which isn't the same as `somewhere/htdocs/tmp/mydata.txt`. Try to change the file name in your write script, and see what happens. And please post the full error message that you get when you move the file. – Sven Jul 09 '15 at 23:24
  • So I changed the name of the file I thought it was writing too and the script doesn't fail ( $ret = file_put_contents('/tmp/mydataa.txt', $data, FILE_APPEND | LOCK_EX); ). No error message still says its writing.. so wheres it trying to write ? It normally fails if the file doesn't exist. – NNNNNNNNNNDelicious Jul 09 '15 at 23:34
  • It's writing to the `/tmp` directory that you may have no access to. Does the file appear somewhere? – Sven Jul 09 '15 at 23:37
  • How do you mean somewhere? – NNNNNNNNNNDelicious Jul 10 '15 at 00:39
  • So you're right I started passing it the full path, and it's working now – NNNNNNNNNNDelicious Jul 10 '15 at 00:51
  • @Sven can you post an answer so I can give you a check mark :) – NNNNNNNNNNDelicious Jul 10 '15 at 00:51
  • 1
    I appreciate your support, but your question would not be helpful to others because it is lacking essential information that came out in the comments, and I'd rather close this question as "off-topic" because the error was a simple one, unlikely to help others. – Sven Jul 10 '15 at 14:18

0 Answers0