PHP zlib

WBOY
풀어 주다: 2024-08-29 13:00:01
원래의
657명이 탐색했습니다.

PHP에서 gzip을 사용하여 압축한 파일을 읽고 쓸 수 있도록 PHP zlib 모듈이라는 모듈을 사용합니다. PHP에서 zlib 모듈을 사용하면 데이터 스트림이 압축되어 내용이 최종 사용자에게 더 빠른 방식으로 제공되고 프로그램에서 zlib 모듈을 활성화하려면 zlib.output_compression = on 줄을 추가해야 합니다. 프로그램과 zlib 모듈은 pligg와 같은 특정 애플리케이션에서 강제로 활성화되어야 하며, 이 모듈은 두 개의 상수, 즉 FORCE_GZIP 및 FORCE_DEFLATE를 정의했습니다. 이는 확장 프로그램이 런타임에 수동으로 로드될 때 사용할 수 있습니다.

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

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

PHP에서 zlib 모듈을 선언하는 구문:

zlib.output_compression = on
로그인 후 복사

PHP에서 zlib 모듈 작업

  • PHP에서는 gzip으로 압축한 파일을 읽고 쓸 수 있도록 PHP zlib 모듈이라는 모듈을 사용합니다.
  • PHP에서 zlib 모듈을 활용하면 데이터 스트림이 압축되기 때문에 최종 사용자에게 더 빠르게 콘텐츠를 제공할 수 있습니다.
  • 프로그램에서 zlib 모듈을 활성화하려면 프로그램에 zlib.output_compression = on 줄을 추가해야 합니다.
  • zlib 모듈은 pligg와 같은 특정 애플리케이션에서 강제로 활성화되어야 합니다.
  • zlib 모듈은 FORCE_GZIP 및 FORCE_DEFLATE라는 두 가지 상수를 정의했으며, 이는 확장 프로그램이 런타임에 수동으로 로드될 때 사용할 수 있습니다.

PHP zlib의 예

다음은 PHP zlib의 예입니다.

예시 #1

gzip을 사용하여 압축된 파일을 투명하게 읽고 쓰는 zlib 모듈을 설명하는 PHP 프로그램.

코드:

<html>
<body>
<?php
#creating a temporary file which is compressed by gzip using tempnum function and storing the path to the file location in a variable called newfile
$newfile = tempnam('/tmp','exfile') . '.gz';
#storing the contents to be written to the file in a variable called data
$data = "Welcome to PHP\n";
#opening the gzip compressed file using gzopen function
$fileopen = gzopen($newfile, "w9");
#writing the contents to the gzip compressed file using gzwrite function
gzwrite($fileopen, $data);
#closing the file after writing the contents to the gzip compressed file
gzclose($fileopen);
#opening the gzip compressed file for reading using gzopen function
$fileopen = gzopen($newfile, "r");
#reading the contents written to the created file using gzread function
echo gzread($fileopen);
#closing the file after reading the contents of the file
gzpassthru($fileopen);
gzclose($fileopen);
echo "\n";
#unlink function is used to delete the file that was just being read
unlink($newfile);
?>
</body>
</html>
로그인 후 복사

출력:

PHP zlib

위 프로그램에서는 tempnum 함수를 사용하여 임시 파일을 생성하는데, 이를 gzip으로 압축하고 파일 위치에 대한 경로를 newfile이라는 변수에 저장합니다. 그러면 gzip으로 압축된 새로 생성된 파일에 쓸 내용이 data라는 변수에 저장됩니다. 그런 다음 쓰기 모드에서 gzopen 함수를 사용하여 gzip으로 압축된 파일을 엽니다. 그런 다음 data 변수에 저장된 내용이 gzwrite 함수를 사용하여 gzip 압축 파일에 기록됩니다. 그런 다음 gzclose 함수를 사용하여 gzip으로 압축된 파일을 닫습니다. 그런 다음 gzopen 함수를 사용하여 gzip으로 압축된 파일을 읽기 모드로 열고 gzread 함수를 사용하여 방금 파일에 쓴 파일의 내용을 읽고 화면에 출력으로 표시합니다. 그런 다음 gzclose 함수를 사용하여 gzip으로 압축된 파일을 닫습니다. 그런 다음 연결 해제 기능을 사용하여 파일을 삭제합니다.

예시 #2

gzip을 사용하여 압축된 파일을 투명하게 읽고 쓰는 zlib 모듈을 설명하는 PHP 프로그램.

코드:

<html>
<body>
<?php
#creating a temporary file which is compressed by gzip using tempnum function and storing the path to the file location in a variable called newfile
$newfile = tempnam('/tmp','exfile') . '.gz';
#storing the contents to be written to the file in a variable called data
$data = "Learning is fun\n";
#opening the gzip compressed file using gzopen function
$fileopen = gzopen($newfile, "w9");
#writing the contents to the gzip compressed file using gzwrite function
gzwrite($fileopen, $data);
#closing the file after writing the contents to the gzip compressed file
gzclose($fileopen);
#opening the gzip compressed file for reading using gzopen function
$fileopen = gzopen($newfile, "r");
#reading the contents written to the created file using gzread function
echo gzread($fileopen);
#closing the file after reading the contents of the file
gzpassthru($fileopen);
gzclose($fileopen);
echo "\n";
#unlink function is used to delete the file that was just being read
unlink($newfile);
?>
</body>
</html>
로그인 후 복사

출력:

PHP zlib

위 프로그램에서는 tempnum 함수를 사용하여 임시 파일을 생성하는데, 이를 gzip으로 압축하고 파일 위치에 대한 경로를 newfile이라는 변수에 저장합니다. 그러면 gzip으로 압축된 새로 생성된 파일에 쓸 내용이 data라는 변수에 저장됩니다. 그런 다음 쓰기 모드에서 gzopen 함수를 사용하여 gzip으로 압축된 파일을 엽니다. 그런 다음 data 변수에 저장된 내용이 gzwrite 함수를 사용하여 gzip 압축 파일에 기록됩니다. 그런 다음 gzclose 함수를 사용하여 gzip으로 압축된 파일을 닫습니다. 그런 다음 gzopen 함수를 사용하여 gzip으로 압축된 파일을 읽기 모드로 열고 gzread 함수를 사용하여 방금 파일에 쓴 파일의 내용을 읽고 화면에 출력으로 표시합니다. 그런 다음 gzclose 함수를 사용하여 gzip으로 압축된 파일을 닫습니다. 그런 다음 연결 해제 기능을 사용하여 파일을 삭제합니다.

장점

  • gzip으로 압축된 파일은 PHP의 zlib 모듈을 이용하여 투명하게 읽거나 쓸 수 있습니다.
  • Zlib 모듈을 사용하여 데이터 스트림을 압축하므로 PHP에서 zlib 모듈을 사용하면 콘텐츠를 최종 사용자에게 더 빠르게 전달할 수 있습니다.
  • zlib 모듈을 사용하면 성능이 훨씬 향상됩니다.

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

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!