0

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

?>
3
  • From the docs ~ mode 'r' - Open for reading only; place the file pointer at the beginning of the file. Commented Oct 20, 2014 at 3:47
  • fopen should create the file if it does not exists . But the file is not being created when i see in the folder Commented Oct 20, 2014 at 3:47
  • There are several modes that will attempt to create the file if it does not exist and you can see them all on the fopen manual page. Hint ~ r is not one of them Commented Oct 20, 2014 at 3:49

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

fopen('demo.txt','w') or die('unable to create'); instead of demo.txt i need to give full path '/var/www/demo.txt' this solved my error

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.