php12345
php12345

Reputation: 31

PHP - Can not open and write to a file using fopen and fwrite in Ubuntu

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.

Upvotes: 2

Views: 1294

Answers (1)

Syscall
Syscall

Reputation: 19780

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");

Upvotes: 3

Related Questions