- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP Create a File
This article is created to cover multiple scripts/programs in PHP, to create a file.
To create a file in PHP, use any of the following modes:
- w
- w+
- a
- a+
- x
- x+
- c
- c+
The last four modes, that are x, x+, c, and c+ only creates a new file, if the specified file does not exits.
PHP Create a File Example
Here is the snapshot of the folder, before executing the PHP script to create a file:
Now the PHP script to create a new file is:
<?php if(fopen("fresherearth.txt", "w")) echo "The file created successfully!"; else echo "The file already exists."; ?>
The output of above PHP example, is:
And here is the new snapshot of the same folder (current directory) after executing the above PHP script of creating a new file:
Note - The fopen() opens a file. And w mode given to this function, creates a new file, then opens that file.
PHP Create a File if Not Exists
This article is created to cover a program in PHP, that creates a new file only if the specified file does not exists.
The x mode is used when we need to create a file only if the specified file does not exists. For example:
<?php $fs = fopen("fresherearth.txt", "x"); if($fs) echo "The file created successfully!"; else echo "The file already exists."; ?>
Since the file fresherearth.txt already exists in the current directory. Therefore, the output produced by above PHP example, is:
The same program can also be created in this way:
<?php if(fopen("fresherearth.txt", "x")) echo "The file created successfully!"; else echo "The file already exists."; ?>
To hide the default error message, use @ character before the fopen() function. For example:
<?php if(@fopen("fresherearth.txt", "x")) echo "The file created successfully!"; else echo "The file already exists."; ?>
Now the output produced by above PHP example, is:
Let me tell you again, if specified file does not exists, then a new file with specified name will get created. For example, let me create another example in which I will provide the name of file, that does not exists in the current directory:
<?php if(@fopen("temp.txt", "x")) echo "The file created successfully!"; else echo "The file already exists."; ?>
Since the file temp.txt is not available in the current directory, therefore temp.txt file will be created, and the output produced by above PHP example should be The file created successfully!
« Previous Tutorial Next Tutorial »