>  기사  >  백엔드 개발  >  PHP 삭제 파일

PHP 삭제 파일

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

PHP에서 파일을 삭제해야 할 때마다 우리는 PHP에서 unlink 함수라는 함수를 사용합니다. 이 함수는 두 개의 매개변수, 즉 filename과 context를 사용합니다. 여기서 filename은 파일이 삭제될 파일 위치의 경로입니다. 삭제된 항목을 찾고 컨텍스트는 파일 스트림의 동작을 변경할 수 있는 옵션 세트인 파일 핸들의 컨텍스트를 나타내며 링크 해제 함수에 대한 이 매개변수는 선택 사항이며 링크 해제 함수가 지정된 파일을 삭제하지 못하는 경우 파일 경로로 지정하면 오류 메시지가 발생합니다.

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

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

구문:

unlink(path_of_the_file, context)

어디,

  • path_of_the_file은 삭제할 파일이 있는 파일 위치의 경로입니다.
  • context는 파일 스트림의 동작을 변경할 수 있는 옵션 집합인 파일 핸들의 컨텍스트를 나타내며 연결 해제 기능에 대한 이 매개 변수는 선택 사항입니다.

PHP에서 Round 함수 작동

  • PHP에서 파일을 삭제해야 할 때마다 PHP에서는 연결 해제 기능이라는 기능을 사용합니다.
  • PHP의 연결 해제 함수는 path_of_the_file과 context라는 두 가지 매개변수를 사용합니다.
  • path_of_the_file 매개변수는 삭제할 파일이 있는 파일 위치의 경로입니다.
  • 매개변수 context는 파일 스트림의 동작을 변경할 수 있는 옵션 집합인 파일 핸들의 컨텍스트를 나타내며 연결 해제 기능에 대한 이 매개변수는 선택 사항입니다.
  • 링크 해제 기능으로 파일 경로에 지정된 파일을 삭제하지 못하는 경우 오류 메시지가 생성됩니다.

PHP 삭제 파일 예시

다음은 언급된 예입니다.

예시 #1

코드:

<html>
<body>
<?php
#a file is opened in write mode using fopen function and the contents are written to the file using fwrite function and then the file is closed using fclose function
$nameofthefile = fopen("filename.txt","w");
echo fwrite($nameofthefile,"Welcome to PHP");
fclose($nameofthefile);
#the file that was created is opened in read mode using fopen function and the contents are read from the file using fread function and displayed as the output on the screenand then the file is closed using fclose function
$fileread = fopen("filename.txt", "r");
echo fread($fileread, filesize("filename.txt"));
fclose($fileread);
#unlink function is used to delete the file that was just being read
unlink("filename.txt");
#again we try to read the contents of the file that was just deleted using unlink function
$fileread = fopen("filename.txt", "r");
echo fread($fileread, filesize("filename.txt"));
fclose($fileread);
?>
</body>
</html>

출력:

PHP 삭제 파일

위 프로그램에서는 fopen 함수를 사용하여 쓰기 모드로 파일을 열고 fwrite 함수를 사용하여 파일에 내용을 쓴 다음 fclose 함수를 사용하여 파일을 닫습니다. 그런 다음 fopen 함수를 사용하여 동일한 파일을 읽기 모드로 열고 fread 함수를 사용하여 파일의 내용을 읽고 파일의 내용을 파일 크기와 함께 화면에 출력한 후 파일을 닫습니다. fclose 함수를 사용합니다. 그런 다음 링크 해제 기능을 사용하여 생성된 동일한 파일을 삭제합니다. 그런 다음 삭제된 파일을 다시 읽으려고 하면 경고 오류 메시지가 화면에 표시됩니다. 출력은 위의 스냅샷에 표시됩니다.

예시 #2

코드:

<html>
<body>
<?php
#a file is opened in write mode using fopen function and the contents are written to the file using fwrite function and then the file is closed using fclose function
$nameofthefile = fopen("filename.txt","w");
echo fwrite($nameofthefile,"Learning is fun");
fclose($nameofthefile);
#the file that was created is opened in read mode using fopen function and the contents are read from the file using fread function and displayed as the output on the screenand then the file is closed using fclose function
$filerad = fopen("filename.txt", "r");
echo fread($fileread, filesize("filename.txt"));
fclose($fileread);
#unlink function is used to delete the file that was just being read
unlink("filename.txt");
#again we try to read the contents of the file that was just deleted using unlink function
$fileread = fopen("filename.txt", "r");
echo fread($fileread, filesize("filename.txt"));
fclose($fileread);
?>
</body>
</html>

출력:

PHP 삭제 파일

위 프로그램에서는 fopen 함수를 사용하여 쓰기 모드로 파일을 열고 fwrite 함수를 사용하여 파일에 내용을 쓴 다음 fclose 함수를 사용하여 파일을 닫습니다. 그런 다음 fopen 함수를 사용하여 동일한 파일을 읽기 모드로 열고 fread 함수를 사용하여 파일의 내용을 읽고 파일의 내용을 파일 크기와 함께 화면에 출력한 후 파일을 닫습니다. fclose 함수를 사용합니다. 그런 다음 링크 해제 기능을 사용하여 생성된 동일한 파일을 삭제합니다. 그런 다음 삭제된 파일을 다시 읽으려고 하면 경고 오류 메시지가 화면에 표시됩니다. 출력은 위의 스냅샷에 표시됩니다.

결론

이 기사에서는 정의, 구문, 예제 및 출력을 통해 연결 해제 함수의 작동을 통해 연결 해제 함수를 사용하여 PHP에서 파일 삭제 개념을 배웠습니다.

위 내용은 PHP 삭제 파일의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

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