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

PHP ファイルの処理

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

最新のソフトウェアはすべて、ファイルを操作する必要があります。ファイル形式で入力を受け入れるか、出力を生成してファイルに追加する必要があります。どちらのシナリオでも、ファイルとの統合機能は、ビジネスの運営に使用されるほぼすべてのソフトウェアに不可欠な機能となっています。どのようなアプリケーションでもファイルの扱いは必要です。一部のタスクを実行するには、ファイルを処理する必要があります。 PHP でのファイル処理は、C などの言語でのファイル処理と似ています。PHP には、使用できる通常のファイル関数が多数あります。

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

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

PHP ファイル処理能力の使用例

例として、銀行は 3 か月または 6 か月の銀行口座明細書などのレポートを作成するのに役立つソフトウェアを必要とし、電子商取引企業は在庫や売上に関連するレポートを印刷する必要があります。株式市場の取引に関連するアプリケーションでは、毎日の株価を読み取り可能なファイルの形式で提供する必要があります。この例では、ビジネス機能をサポートするソフトウェアではファイルへのデータの読み取りまたは書き込みが必要になることに同意するはずです。

現代のアプリケーションではファイル処理能力がほぼ必須であるため、Python、Java、C#、PHP などのすべての著名なプログラミング言語には、開発者が対話型アプリケーションを開発するために利用するファイル処理機能が組み込まれています。

PHP のファイル処理機能

PHP は、読み取りおよび書き込み操作のために次のファイル形式をサポートしています。

  • テキスト ファイル: 拡張子 .txt のファイル
  • ログ ファイル: 拡張子 .log を持つファイル
  • カスタム拡張子: .abc などのカスタム拡張子を持つファイル
  • CSV ファイル: 拡張子 .csv を持つファイル
  • 画像ファイル: 拡張子 .jpg/png/gif を持つファイル
  • 初期化設定を持つファイル: 拡張子 .ini を持つファイル

PHP のファイル処理関数

PHP には、さまざまなファイル操作を実行するための幅広い組み込み関数が用意されています。これらのファイル関数は、Linus、Unix、MAC、Windows などのすべての OS システムで適切に動作します。ただし、MAC OS と Windows のファイル名では大文字と小文字が区別されませんが、Unix と Linux では大文字と小文字が区別されます。したがって、混乱やエラーを避けるために、プラットフォームの完全な互換性を確保するために、すべてのファイルに小文字で名前を付けることがベスト プラクティスと考えられます。

PHP ファイル処理関数がどのように機能するかについて大まかに理解できたので、これらの関数を 1 つずつ理解してみましょう。

1. file_exists() 関数

この関数は、パラメータとして指定されたファイル名の存在を確認するために使用されます。これは、存在しないファイルの読み取りまたは書き込みを試みることによって発生する可能性のあるエラーを回避するために使用されます。

構文:

<?php
file_exists($file_name) //where file_name  would be a file with one of the supported extensions
?>

file_exists() は、ファイルが存在する場合は True 値を返し、ファイルが存在しない場合は false を返します。

次に、コード仕様でこの関数を使用して、ファイルの存在を確認してみましょう。ルートフォルダーに「mysettings.ini」という名前のファイルを配置し、次のコードでアクセスしてみます。

コード:

<?php
if (file_exists('mysettings.ini))
{
echo 'yay! file found!';
}
else
{
echo 'Sorry! mysettings.ini does not exist';
}
?>

出力:

PHP ファイルの処理

ここで、その場所からファイルを削除して上記のコードを実行すると、次の出力が表示されます。

PHP ファイルの処理

2. fopen() 関数

fopen 関数は、アプリケーションが読み取るファイルを開くために php で使用されます。

構文:

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

上記の構文では、$fname はファイル名を表し、$mode はファイルを開くモードを表します。 $mode は次のいずれかの値です。

  • r: For opening the file in only read-only mode. It returns false if the file name supplied is not found on the location supplied.
  • r+: For opening the file in both read and write mode. Similar to ‘r’, it also returns false if a file is not found.
  • w: For opening the file in only write-only mode. If the file supplied doesn’t exist, it attempts to create one.
  • w+: For opening the file in both read and write mode. Similar to ‘w’, it also attempts to create a file if the file name supplied is not found.
  • a: For opening the file in write-only mode and appending to the end of the file. If the file supplied doesn’t exist, it attempts to create one.
  • a+: For opening the file in both read and write mode. Similar to ‘a’, it also attempts to create a file if the file name supplied is not found.

3. fwrite() Function

As the name suggests, this function is used to write content to files.

Syntax:

<?php
fwrite($handle, $data_string, $len);
?>

Where $handle is the file location, $data_string is the text string we would like to write to the file and $len is the optional parameter to specify the maximum length of the file.

4. fclose() Function

The fclose() function is used in php when the read/write operations on file are completed and we would like to close the file.

Syntax:

<?php
fclose($file_handle);
?>

Where $file_handle stands for the file pointer.

5. fgets() Function

The fgets() function is used in php to read the file line by line.

Syntax:

<?php
fgets($file_handle);
?>

Where $file_handle stands for the file pointer.

6. copy() Function

The copy() function allows us to copy a file in php.

Syntax:

<?php
copy($file1, $file2);
?>

Where $file1 is the original file and $file2 is the copied file location.

7. unlink() Function

The unlink() function in Php is used to delete a file.

Syntax:

<?php
unlink($filename);
?>

Where the $filename is the filename to be deleted.

Conclusion

With the above example, we can easily conclude that php has a wide variety of in-built functions that simplify reading and writing operations on the file. The most commonly used function include fopen() to open the file in different modes, fwrite() to write data to the file, fread() to read the file content, fclose() to close the file once the necessary operation is done, copy() to copy one file content to another and unlink to delete the unwanted files.

以上がPHP ファイルの処理の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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