All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

PHP Create File If Not Exists

Last Updated : Mar 11, 2024

PHP Create File If Not Exists

In this article we will show you the solution of PHP create file if not exists, do you felt this before created duplicate files without our knowledge? Yeah! No worries this blog help to resolve it by the right condition.

To execute this program correctly you should know file creation as foremost step.

Anyone can easily create file with the help of file_put_contents() method.

This is an inbuilt function that helps user who are wants to create a file.

Besides, this function will work properly when your local storage not have file with same name previously.

Step By Step Guide On PHP Create File If Not Exists :-

To achieve the result firstly we you need to create a variable to hold a text file with extension.

Everyone know if() statement use, exactly here we used this if to identify whether a file existed or not.

Subsequently, we define within if condition block that will work when your local storage not have you defined file.

Otherwise it won’t do anything as your imagination like create one more time.

If not, this file is fresh then it will print a given state as first value to that file.

If you want to write any other text to your file during creation you have to change this line as your wish.

Then remember to pass these two into file_put_contents() method to reach the target.

    $cnt='Hi This Is New File';
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
  3. We created variable ‘$f’ to specify or store file name that will created when you execute this program. Without modification running this program give you result as local storage creates new text file with name ‘Hi’.
  4. Here, you can change this file name with anything according to your requirement. Then if() condition was defined with statement !is_file. There value ‘$f’ file passed to it to verify if this file not created if block code will executed successfully.
  5. Already ‘Hi’ text present in your locale storage, then it wont work. If ‘Hi’ name text file is not presented then its created as newly into your local storage. Variable $cnt defined to write any sentence or text to creating file, so you has access to change value ‘Hi This Is New File’.
  6. This is achieved by file_put_contents() method unless nothing will happen as your think.
  7. Instead echo() you can prefer print() method if you want and that not going to affects your result. Still some type of input exist you can try those along with it to master your mind in this concept.

Conclusion :-

In conclusion, hope now you know how to check file and create file with a sentence in php.

When you work with php we need to create and process php files at server location and then we need to start the server before execute the program.

Otherwise sometimes you will get error so ensure it if you get any error while running output.

When we execute this program on browser it will print not show anything but if you want notification you can add a echo statement with text ‘file created successfully’ as output.

With this help of sample code you can test with any other file names and you will get result according to your changes.

I hope this article on PHP create file if not exists helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪