2

I'm using Ubuntu 16.04 and I'm trying the following code to write to a file.

<?php

$path = getcwd();
$fp = fopen($path . 'data.txt', 'w') or die("Can not open the file");
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);

Initially the file permissions on data.txt was -rw-r--r-- and causing the following error:

fopen(data.txt): failed to open stream: Permission denied

So I changed file permissions to -rw-rw-rw-, but nothing is happening now. It is neither writing to a file nor showing any error.

Can anybody please help me what has went wrong here?

Thanks in advance.

1 Answer 1

3

getcwd() does not contain the final slash. So you have to add it :

$fp = fopen($path . '/data.txt', 'w') or die("Can not open the file");
Sign up to request clarification or add additional context in comments.

1 Comment

Oh yes, slash was missing. Thanks a lot for noticing it. It is now working. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.