PHP file functions are the best and the convenient way of working with files we have with the help of PHP’s huge collection of the built-in functions. Windows Operating System and MAC Operating Systems are not the cases sensitive. Adopting to the lower case letters naming conversion for the file naming purpose is the best practice that ensures the maximum cross platform’s compatibility. There are some PHP File Functions that are very much helping in handling the data which is present in the file information.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
PHP File Functions help in-store/delete/manipulate/copy the data in the file or deleting the file etc. Here is the list of some of the file functions. They are:
Below are the examples of PHP file Functions:
In order to write something in the file or manipulate the data in the delete however you want then at first, you have to check whether the file exists in the directory or not in order to process it. This PHP function also helps you in creating a new if the file you search is not present in the server and you want to create a new file at the server.
Syntax:
Explanation:
“file_exists()” function is a PHP Function which returns the result as TRUE only if the file exists in the server or the result will FALSE if the file doesn’t exist/found in the server/server directory. $file_name variable is the file path and the name of the file at the end of the path which is to check.
Example:
This is the below example that uses the file_exists() function in order to determine the file whether it exists or not. Save the code below in the file_function.php which is in the syntax and open the file path in the browser so that you will see the result/output. File_name.txt is not created so the output will be the result of FALSE and the output of the ELSE condition statement will be the result.
Code:
Output:
PHP fopen function will help you to open the file/files which are in the server.
Syntax:
Explanation:
Example:
The below syntax just open’s the file which has the name as file_name.txt and if not found then out will print which is in the die() function and die() function will be executed when the error occurs. Die() will display the message which is existed inside of the parenthesis. So no output in the browser mostly if the file really exists.
Code:
PHP write function will help you to write files.
Syntax:
Explanation:
Fclose Function will help to close the file which is opened already in the server.
Syntax:
Explanation:
PHP Fgets Functions will help to read the file/files are red line by line using the syntax:
fgets($handle);
Code:
PHP copy function will be used in order to copy the files.
Syntax:
copy($file, $file_copied);
Explanation:
Code:
This function helps in reading the entire contents of the file. Difference between the fgets and file_get_contents will return the whole data as a string but the fgets will be red the whole file line by line.
Code:
"; // Enables the display of the line feeds echo file_get_contents("file_name.txt"); echo ""; // Now it Terminates the pre tag ?>
Unlink Function will help to delete a file.
Code:
All PHP File Functions help in supporting the wide range of some of the file formats. They are:
This is a guide to PHP file Functions. Here we discuss the Introduction to PHP file Functions examples along with code implementation and output. You can also go through our other suggested articles to learn more –
以上是PHP file Functions的詳細內容。更多資訊請關注PHP中文網其他相關文章!