html5-img
1 / 9

PHP  ファイルオープン

PHP  ファイルオープン. PHP でファイルを開くには( 1/2 ). fopen 関数を使う方法① fopen( “ ファイル名(パス名) ” , “ 開き方 ” ) ; 戻り値:ファイルポインタ file 関数を使う方法② file( “ ファイル名(パス名) ” ) ; 戻り値:行ごとの文字列を保持した配列 ファイル名の例 /home/fushimi/test.txt http://ai.u-shizuoka-ken.ac.jp/test.txt 開き方 読み込み:“ r”   書き込み:“ w”   など. PHP でファイルを開くには( 2/2 ).

beryl
Download Presentation

PHP  ファイルオープン

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP ファイルオープン

  2. PHPでファイルを開くには(1/2) • fopen関数を使う方法① • fopen(“ファイル名(パス名)”, “開き方”); • 戻り値:ファイルポインタ • file関数を使う方法② • file(“ファイル名(パス名)”); • 戻り値:行ごとの文字列を保持した配列 ファイル名の例 • /home/fushimi/test.txt • http://ai.u-shizuoka-ken.ac.jp/test.txt 開き方 • 読み込み:“r”  書き込み:“w”  など

  3. PHPでファイルを開くには(2/2) • file_get_contents関数を使う方法③ • file_get_contents(“ファイル名(パス名)”); • ファイル全体を文字列として読み込む • 戻り値:ファイル全体

  4. ファイルの内容の読み込み • fgets関数 • fgets(ファイルポインタ); • ファイルの1行を読み込む • 戻り値:ファイルの1行分のデータ • fread関数 • fread(ファイルポインタ,読み込む長さ); • バイナリファイルを開く時は、fopenで“rb”指定 • 戻り値:読み込んだ文字列

  5. ファイルへの書き込み • fwrite関数(fput関数) • fwrite(ファイルポインタ, “書き込む文字列”); • 戻り値:書き込まれた文字列のサイズ 又はFALSE

  6. ファイルを閉じる • fopenで開いたファイルは閉じる必要がある • fclose関数 • fclose(ファイルポインタ); • 戻り値:成功したらTRUE、失敗したらFALSE

  7. その他(ファイルシステム関数) • feof関数 • feof(ファイルポインタ); • ファイルポインタが終端(EOF:End Of File)に達したかをどうかを調べ • 戻り値:端に達した場合はTRUE、 終盤に達していない場合はFALSE

  8. その他(文字列処理関数) • explode関数 • explode(“区切り文字”, “対象文字列”); • 区切り文字:スペース、カンマ、コロン、タブなど • 戻り値:分割された文字列を要素とする配列 • 例: • $data = "ABC,DEF,GHI";$array = explode(",", $data); 0 1 2 • 結果  $array ⇒

  9. fgetsの具体例 $i=0; while(!feof(ファイルポインタ)){ $row[$i]=fgets(ファイルポインタ); $i++; } ○○ △△ ×× $row[] $i=0 $i=1 $i=2

More Related