>  기사  >  백엔드 개발  >  PHP 파일 기능

PHP 파일 기능

王林
王林원래의
2024-08-29 13:02:25833검색

PHP 파일 기능은 PHP에 내장된 다양한 기능 모음을 통해 파일 작업을 수행하는 가장 좋고 편리한 방법입니다. Windows 운영 체제와 MAC 운영 체제는 대소문자를 구분하지 않습니다. 파일 이름 지정 목적으로 소문자 이름 변환을 채택하는 것은 크로스 플랫폼의 호환성을 최대화하는 모범 사례입니다. 파일 정보에 존재하는 데이터를 처리하는 데 매우 도움이 되는 몇 가지 PHP 파일 함수가 있습니다.

광고 이 카테고리에서 인기 있는 강좌 PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

PHP 파일 기능

PHP 파일 기능은 파일의 데이터를 저장/삭제/조작/복사하거나 파일 삭제 등을 도와줍니다. 다음은 일부 파일 기능 목록입니다. 그들은:

  1. file_exists 함수
  2. fopen 함수
  3. fwrite 함수
  4. fclose 함수
  5. fgets 함수
  6. 복사기능
  7. file_get_contents 함수와
  8. 파일 삭제

PHP 파일 기능 구현 예시

다음은 PHP 파일 함수의 예입니다.

1. PHP 파일_존재함수

파일에 무언가를 쓰거나 삭제된 데이터를 마음대로 조작하려면 먼저 해당 파일이 디렉터리에 있는지 확인하고 처리해야 합니다. 이 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" 모드는 파일에 일부 데이터를 쓰는 데 도움이 됩니다. 파일을 길이가 0이 되도록 자릅니다. 파일이 존재하지 않는 경우 읽기 및 쓰기가 아닌 쓰기 전용 파일이 생성됩니다. 읽고 쓰기 위해서는 “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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP 암호화다음 기사:PHP 암호화