찾다
php教程PHP源码php读取文件与写入文件方法实例

在php中读取文件我们需要使用file_get_contents或fopen来打开文件然后再读取了,file_get_contents函数读文件比fopen要方便,写文件需要fopen函数与file_put_contents或fwrite合作才可以实例下面我来介绍下。

<script>ec(2);</script>

php写文件的方法

实例一

 代码如下 复制代码
$filename = 'test.txt';
$filename = dirname ( __FILE__ ) . '/' . $filename;
if (file_exists ( $filename )) {
    if (! is_writable ( $filename )) {
        exit('is not writable');
    }
    $handle = fopen ( $filename, "a+b" );
    $content = 'this is test words';
    $content .= "n";
    fwrite($handle, $content);
    fclose ( $handle );
} else {
    exit('file is not exists');
}

实例二

 代码如下 复制代码

$filename = 'test.txt';
$filename = dirname ( __FILE__ ) . '/' . $filename;
if (file_exists ( $filename )) {
    if (! is_writable ( $filename )) {
        exit('is not writable');
    }
    $content = 'this is test words';
    $content .= "n";
    file_put_contents($filename, utf8_encode($content)); 
} else {
    exit('file is not exists');
}


php读取文件

实例一

 代码如下 复制代码

$filename = 'test.txt';
        $filename = dirname ( __FILE__ ) . '/' . $filename;
        if (file_exists ( $filename )) {
            if (! is_readable ( $filename )) {
                exit('is not readable');
            }
            $contents = file_get_contents($filename);
            $contents = explode("n", $contents);
            print_r($contents);
        } else {
            exit('file is not exists');
        }

实例二

 代码如下 复制代码

$filename = 'test.txt';
$filename = dirname ( __FILE__ ) . '/' . $filename;
if (file_exists ( $filename )) {
    if (! is_readable ( $filename )) {
        exit('is not readable');
    }
    $handle = fopen ( $filename, "rb" );
    $contents = fread($handle, filesize ($filename));
    //$contents = stream_get_contents($handle); // 也可以用方法替换上一行
    $contents = explode("n", $contents);
    fclose ( $handle );
    print_r($contents);
} else {
    exit('file is not exists');
}

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

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

ZendStudio 13.5.1 맥

ZendStudio 13.5.1 맥

강력한 PHP 통합 개발 환경

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구