PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

HOW TO CREATE A FILE IN PHP

PHP has a host of file functions enabling you to do almost anything with files.

For example, you can create a file from scratch with the following code:

<?

$fh=fopen("myfile.txt", "w") or die("can't open file");

fclose($fh);

?>

PHP will then create this file myfile.txt in the same directory as this php script used to create the file. An explanation of the code as follows:

We assign the $fh variable to the result of the fopen function. The fopen function takes two parameters, the first is the name of the file and the second is the opening mode. The w tells php to open and therefore create the file in write mode. If the file cannot be created, an error message can't open file will be displayed.

$fh=fopen("myfile.txt", "w") or die("can't open file");

After the line above, the file myfile.txt is created. We then tell PHP to close the file as follows:

fclose($fh);

Home | Privacy Policy | Terms Of Use | Contact Us