简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM