Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP create file in same directory as script

Tags:

php

cron

cpanel

$content = "some text here"; 
$fp = fopen("myText.txt","w"); 
fwrite($fp,$content); 
fclose($fp);

The above code creates a file in the folder where the PHP script is present. However when the script is called by Cpanel Cron then file is created in home directory.

I want file to be created in the same folder where the php script is present even if its run by cron.

How to do that ?

like image 993
Greatchap Avatar asked Jan 26 '18 08:01

Greatchap


People also ask

How can create file in specific folder in PHP?

PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

How do you read or write a file on the server from PHP give example?

Open a file using fopen() function. Get the file's length using filesize() function. Read the file's content using fread() function. Close the file with fclose() function.

Which is the correct way to include a file text PHP in PHP code?

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

How do I write a PHP document?

In this article, we are going to discuss how to write into a text file using the PHP built-in fwrite() function. The fwrite() function is used to write into the given file. It stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first.


1 Answers

Try using __DIR__ . "/myText.txt" as filename.
http://php.net/manual/en/language.constants.predefined.php

like image 147
Tekk Avatar answered Sep 22 '22 06:09

Tekk