PHP
PHP String Functions: substr, strlen, strtolower, explode, strpos, str_replace
What is a string? A string is a collection of characters. String is one of the data types...
A file is simply a resource for storing information on a computer.
Files are usually used to store information such as;
In this tutorial, you will learn-
PHP file functions support a wide range of file formats that include;
PHP provides a convenient way of working with files via its rich collection of built in functions.
Operating systems such as Windows and MAC OS are not case sensitive while Linux or Unix operating systems are case sensitive.
Adopting a naming conversion such as lower case letters only for file naming is a good practice that ensures maximum cross platform compatibility.
Let’s now look at some of the most commonly used PHP file functions.
This function is used to determine whether a file exists or not.
The file_exist function has the following syntax.
<?php file_exists($filename); ?>
HERE,
The code below uses file_exists function to determine if the file my_settings.txt exists.
<?php if (file_exists('my_settings.txt')) { echo 'file found!'; } else { echo 'my_settings.txt does not exist'; } ?>
Save the above code in a file named file_function.php Assuming you saved the file in phptuts folder in htdocs, open the URL http://localhost/phptuts/file_function.php in your browser You will get the following results.
The fopen function is used to open files. It has the following syntax
<?php fopen($file_name,$mode,$use_include_path,$context); ?>
HERE,
Mode | Description |
---|---|
r |
|
r+ |
|
w |
|
w+ |
|
a |
|
a+ |
|
The fwrite function is used to write files.
It has the following syntax
<?php fwrite($handle, $string, $length); ?>
HERE,
Is is used to close a file in php which is already open
It has the following syntax.
<?php fclose($handle); ?>
HERE,
Let’s now look at an example that creates my_settings.txt.
We will use the following functions.
The code below “create_my_settings_file.php” implements the above example.
Open a file | <?php $fh = fopen("my_settings.txt", 'w') or die("Failed to create file"); ?> |
Closing a file | <?php fclose($fh); ?> |
Create File | <?php $fh = fopen("my_settings.txt", 'w') or die("Failed to create file"); $text = <<<_END localhost;root;pwd1234;my_database _END; fwrite($fh, $text) or die("Could not write to file"); fclose($fh); echo "File 'my_settings.txt' written successfully"; ?> |
Open the URL http://localhost/phptuts/create_my_settings.php in your browser.
You will get the following page
Note: if your disk is full or you do not have permission to write files, you will get an error message.
Switch back to the URL http://localhost/phptuts/file_function.php .
What results do you get?
The fgets function is used to read php files line by line. It has the following basic syntax. fgets($handle); HERE,
Let’s now look at an example that reads my_settings.txt file using the fopen and fgets functions.
The code below read_my_settings.php implements the above example.
<?php $fh = fopen("my_settings.txt", 'r') or die("File does not exist or you lack permission to open it"); $line = fgets($fh); echo $line; fclose($fh); ?>
HERE,
The PHP copy function is used to copy files. It has the following basic syntax. copy($file,$copied_file); HERE,
The code below illustrates the implementation
<?php copy('my_settings.txt', 'my_settings_backup.txt') or die("Could not copy file"); echo "File successfully copied to 'my_settings_backup.txt'"; ?>
The unlink function is used to delete the file. The code below illustrates the implementation.
<?php if (!unlink('my_settings_backup.txt')) { echo "Could not delete file"; } else { echo "File 'my_settings_backup.txt' successfully deleted"; } ?>
The file_get_contents function is used to read the entire file contents.
The code below illustrates the implementation.
The difference between file_get_contents and fgets is that file_get_contents returns the file data as a string while fgets reads the file line by line.
<?php echo "<pre>"; // Enables display of line feeds echo file_get_contents("my_settings.txt"); echo "</pre>"; // Terminates pre tag ?>
Function | Description |
---|---|
File_exists | Used to determine if a file exists or not |
fopen | Used to open a file. Returns a pointer to the opened file |
fwrite | Used to write to files |
fclose | Used to open closed files |
fgets | Used to read a file line by line |
copy | Used to copy an existing file |
unlink | Used to delete an existing file |
file_get_contents | Used to return the contents of a file as a string |
What is a string? A string is a collection of characters. String is one of the data types...
Following are frequently asked Laravel and PHP related interview questions for freshers as well as...
What is a Function? A function is a reusable piece or block of code that performs a specific...
Training Summary PHP is the most popular scripting language on the web. Without PHP Facebook,...
A Loop is an Iterative Control Structure that involves executing the same number of code a number...
In this tutorial, you will learn- PHP Data Types PHP Variable Use of variables Variable type...