PHP file Functions

王林
發布: 2024-08-29 13:02:25
原創
734 人瀏覽過

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

PHP file Functions

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:

  1. file_exists Function
  2. fopen Function
  3. fwrite Function
  4. fclose Function
  5. fgets Function
  6. copy Function
  7. file_get_contents Function and
  8. deleting a File

Examples to Implement PHP file Functions

Below are the examples of PHP file Functions:

1. PHP file_exists Function

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 file Functions

2. PHP fopen Function

PHP fopen function will help you to open the file/files which are in the server.

Syntax:

登入後複製

Explanation:

  • “fopen” is the PHP file function which is used to open the file which is in the server/server directory.
  • “$file_name” is the actual file name which is to open
  • “mode” is like what you want to do with the file like reading, writing, appending, etc.
  • Mode “r” will read the file from the beginning and returns false if the file don’t even exist. It helps to read-only rather than read and write mode. For read and write mode, one must use “r+” mode.
  • Mode “w” will helps to write some data to the file. It will truncate the file to the zero-length. If the file doesn’t even exist then the file will be created to write only rather than read and write. In order to read and write “w+” mode will be used.
  • Mode “a” will appends the file at the end. If the file doesn’t even exist then the file will be created with write only mode. For read and write mode of appending then “a+” mode will be used.
  • “$use_include_path” is the optional term and by default the result is false, if it is set to the TRUE result then the functions help the include path which is present too. Likewise “$context” is also optional which can be used in order to specify the context support.

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:

登入後複製

3. PHP write Function

PHP write function will help you to write files.

Syntax:

登入後複製

Explanation:

  • “fwrite” PHP function will help to write some data to the files.
  • “$handle” term is the file pointer’s resource.
  • “$string” term is the data/information which is to be written inside the file.
  • “$length” term is optional which helps to specify the maximum file length.

4. PHP Fclose Function

Fclose Function will help to close the file which is opened already in the server.

Syntax:

登入後複製

Explanation:

  • “fclose” will helps you to close the function which is opened already in the server/server directory.
  • “$handle” is the pointer’s resource of the file.

5. PHP fgets Function

PHP Fgets Functions will help to read the file/files are red line by line using the syntax:

fgets($handle);
登入後複製
  • “$fgets” is to read the lines of the file.
  • “$handle” is the resource of the file pointer.

Code:

登入後複製

6. PHP Copy Function

PHP copy function will be used in order to copy the files.

Syntax:

copy($file, $file_copied);
登入後複製

Explanation:

  • “$file” is the path of the file which is to be copied.
  • “$file_copied” term is the name of the copied file.

Code:

登入後複製

7. PHP file_get_contents Function

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 ?>
登入後複製

8. Deleting a File (Unlink Function)

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:

  • File_name.txt
  • File_name.log
  • File_name.custom_extension i.e., file_name.xyz
  • File_name.csv
  • File_name.gif, File_name.jpg, etc.
  • Files/File provides permanent cost-effective data storage solution/solutions for the simple data when compared to the databases which require some software and some skills in order to manage the Database Management Systems(DBMS Systems).
  • File Functions helps to store some simple data like the server logs in order to analyze the data or for retrieving the data for future purpose.
  • PHP file functions will help you to store the program/program settings which are like program.ini etc.

Recommended Article

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 –

  1. Factorial in PHP
  2. PHP Pagination
  3. PHP unset()
  4. PHP MD5()

以上是PHP file Functions的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!