ashwinbhy
ashwinbhy

Reputation: 600

Unable to create a file on ubuntu using php

I am trying to create a simple text file on my server using PHP.

I have given 777 permission to the folder still I am unable to create the file it gives following error:

Warning: fopen(demo.txt): failed to open stream: No such file or directory in /var/www/code/fcreate.php on line 6 unable to create

The PHP code is as follows:

<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

fopen('demo.txt','r') or die('unable to create');

?>

Upvotes: 0

Views: 898

Answers (1)

Kevin
Kevin

Reputation: 41885

You're trying to read a file because of that flag you're using r (read).

The flag r stands for:

'r' Open for reading only; place the file pointer at the beginning of the file.

You can use a+ read/write. If the file does not exist, attempt to create it.

$handle = fopen('demo.txt','a+') or die('unable to create');

If you want more clarity. Kindly visit the manual for more details. (Check out the modes part).

Upvotes: 2

Related Questions