ホームページ  >  記事  >  バックエンド開発  >  PHPファイルの機能

PHPファイルの機能

王林
王林オリジナル
2024-08-29 13:02:25835ブラウズ

PHP ファイル関数は、PHP の膨大な組み込み関数のコレクションを利用して、ファイルを操作するための最良かつ便利な方法です。 Windows オペレーティング システムと MAC オペレーティング システムでは、大文字と小文字が区別されません。ファイル命名目的で小文字の命名変換を採用することは、クロスプラットフォームの互換性を最大限に確保するためのベスト プラクティスです。ファイル情報に存在するデータの処理に非常に役立つ PHP ファイル関数がいくつかあります。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

PHP ファイルの関数

PHP ファイル関数は、ファイル内のデータの保存/削除/操作/コピーやファイルの削除などに役立ちます。ファイル関数の一部のリストは次のとおりです。それらは次のとおりです:

  1. file_exists 関数
  2. fopen 関数
  3. fwrite 関数
  4. fclose 関数
  5. fgets 関数
  6. コピー関数
  7. file_get_contents 関数と
  8. ファイルの削除

PHP ファイル関数の実装例

以下は PHP ファイル関数の例です:

1. PHP file_exists 関数

ファイルに何かを書き込んだり、削除でデータを操作するには、まず、ファイルを処理するためにディレクトリにファイルが存在するかどうかを確認する必要があります。この PHP 関数は、検索したファイルがサーバーに存在せず、サーバーで新しいファイルを作成したい場合に、新しいファイルを作成するのにも役立ちます。

構文:

<?php
file_exists($file_name);
?>

説明:

「file_exists()」関数は、ファイルがサーバーに存在する場合にのみ結果を TRUE として返し、ファイルがサーバー/サーバー ディレクトリに存在しない/見つからない場合には結果を FALSE として返す PHP 関数です。 $file_name 変数は、ファイル パスと、チェックするパスの末尾にあるファイルの名前です。

例:

これは、ファイルが存在するかどうかを判断するために file_exists() 関数を使用する以下の例です。構文内の file_function.php に以下のコードを保存し、ブラウザーでファイル パスを開くと、結果/出力が表示されます。 File_name.txt は作成されないため、出力は FALSE の結果となり、ELSE 条件ステートメントの出力が結果となります。

コード:

<?php
If(file_exists('file_name.txt'))
{
echo "Now the File Found!!!";
}
else{
echo "your file_name.txt doesnot exist until now";
}
?>

出力:

PHPファイルの機能

2. PHP fopen 関数

PHP の fopen 関数は、サーバーにあるファイルを開くのに役立ちます。

構文:

<?php
fopen($file_name, $mode, $use_include_path,$context);
?>

説明:

  • 「fopen」は、server/server ディレクトリにあるファイルを開くために使用される PHP ファイル関数です。
  • 「$file_name」は、開く実際のファイル名です
  • 「モード」は、読み取り、書き込み、追加など、ファイルに対して実行したいことのようなものです。
  • モード「r」はファイルを最初から読み取り、ファイルが存在しない場合は false を返します。読み取り/書き込みモードではなく読み取り専用の方が役立ちます。読み取りおよび書き込みモードの場合は、「r+」モードを使用する必要があります。
  • モード「w」は、ファイルへのデータの書き込みに役立ちます。ファイルは長さゼロに切り詰められます。ファイルが存在しない場合、ファイルは読み取りと書き込みではなく書き込み専用として作成されます。読み書きには「w+」モードが使用されます。
  • モード「a」はファイルを最後に追加します。ファイルが存在しない場合、ファイルは書き込み専用モードで作成されます。追加の読み取りおよび書き込みモードの場合は、「a+」モードが使用されます。
  • 「$use_include_path」はオプションの用語であり、デフォルトでは結果は false になります。結果が TRUE に設定されている場合、関数は存在するインクルード パスもサポートします。同様に、「$context」もオプションで、コンテキストのサポートを指定するために使用できます。

例:

以下の構文は、file_name.txt という名前のファイルを開くだけです。見つからない場合は、die() 関数内にあるファイルが出力され、エラーが発生したときに die() 関数が実行されます。 Die() は括弧内にあるメッセージを表示します。そのため、ファイルが実際に存在する場合、ほとんどの場合、ブラウザーには出力がありません。

コード:

<?php
$op = fopen("file_name.txt",'w');
or
die("Now we are failed in creating the file");
?>

3. PHP書き込み関数

PHP の書き込み関数はファイルの書き込みに役立ちます。

構文:

<?php
fwrite($handle,$string,$length);
?>

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:

<?php
fclose($handle);
?>

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:

<?php
$op = fopen("file_name.txt",'r');
or
die("Now we are failed in opening the file");
$line1 = fgets(#op);
echo $line1;
fclose($op);
?>

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:

<?php
copy('file_name.txt','my_backup_settings.txt')
or
die("We can't cop the file");
echo "File now successfully copied to the 'my_backup_settings.txt'";
?>

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:

<?php
echo "<pre class="brush:php;toolbar:false">"; // 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:

<?php
if(!unlink('my_backup_settings.txt'))
{
echo " Cannot delete the file";
}
else
{
echo "file 'my_backup_settings.txt' now deleted successfully";
}
?>

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ファイルの機能の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:PHP暗号化次の記事:PHP暗号化