LoginSignup
0
0

More than 3 years have passed since last update.

#fopenによるファイルアクセス、fgetsによるファイルの読み込み、fputsによるファイルの保存、インターネット上のファイルを読み込み

Posted at

fopenによるファイルアクセス、fgetsによるファイルの読み込み

index.phpを以下のように編集

<?php
 $f=@fopen("data.txt",'r')or exit("BREAK");
 $result='';
 while(!feof($f)){
   $result.=fgets($f,10);
 }
 fclose($f);

?>

<!DOCTYPE html 
          PUBLIC" -//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="ja" lang="ja">
<head>
    <meta http-equiv="Content=Type" content="text/html; charset=UTF-8"/>
    <title>sample page</title>
    <style>
        h1{font-size:18pt;font-weight:bold;color:#666;}
        body{fontsize:13pt;color:#666;margin5px 25px;}
        tr{margin:5px;}
        th{padding:5px;color:white;background:darkgray;}
        td{padding:5px;color:black;background:#e0e0ff;}
    </style>
</head>
<body>
     <h1>Hello PHP!</h1>
    <p><?php echo $result; ?></p>
</body>      
</html>

Googlechromeで「http://localhost/sample/index.php」にアクセス後
data.txt内にある値が表示されていることを確認する

image.png

今回使用した関数
 ・fopen
  $変数名 = fopen(ファイルの指定、モード指定);
  引数に指定したファイルを開き、そのファイルにアクセスするための「ファイルポインタ」
  と呼ばれるものを返します。ファイルにアクセスする場合は駆らなずfopenでファイルを開きます。
  2つ目の引数にある「モード指定」はファイルのアクセスモードというものを指定するものです。
  アクセスモードとは、そのファイルをどういう種類のファイルとして扱うか、どういうアクセスを許可
  するかといった設定を行うものです。
  
  ・fopenのモード指定用の記号
  r:読み込み専門で開く
  r+:読み込み・書き出し用に開く
  w:書き出し専門で開く
  w+:読み込み書き出し用に開く
  a:追記モードの書き出し専門で開く
  a+:追記モードの読み込み・書き出し用に開く
  x:書き出し専門で開く、すでにファイルがある場合にはエラーになる
  x+:読み込み・書き出しように開く、すでにファイルがある場合にはエラーになる
  b:バイナリモードで開く
  t:テキストモードで開く
 
 ・fgets
  $変数=fgets(ファイルポインタ);
  $変数=fgets(ファイルポインタ,バイト数);
  
  ファイルのデータを読み込む処理、引数には読み込むファイルポインタを指定します。2番目の引数
  がないと、開業まで読み込みます。2番目の引数に整数を指定すると、そのバイト数だけ読み込みます。
  

fputsによるファイルの保存

index.phpを以下のように編集

<?php
IF($_POST !=null){
     $f=@fopen("data.txt",'a')or exit("ファイルが読めません");
if ($f != null){ 
    $s = $_POST['text1']; 
    fputs($f,$s."\n"); 
    fclose($f);
  }
}
 $f2 = @fopen("data.txt",'r')or exit("ファイルが読み込めません");
 $i=1;
 while(!feof($f2)){
     $s2=htmlspecialchars(fgets($f2));
     if($s2 !=""){
         $result=$i++.":".$s2."<br>".$result;
     }
 }
 fclose($f2);                        
?>

<!DOCTYPE html 
          PUBLIC" -//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="ja" lang="ja">
<head>
    <meta http-equiv="Content=Type" content="text/html; charset=UTF-8"/>
    <title>sample page</title>
</head>
<body>
     <h1>Hello PHP!</h1>
    <form method="post" action="./index.php">
    <input type="text" name="text1">
    <input type="submit">
    </form>
    <hr>
    <p><?php echo $result;?></p>

</body>      
</html>

 Googlechromeで「http://localhost/sample/index.php」にアクセス後入力欄に任意の文字を入力して送信ボタンを押下
 データが反映されていることを確認する
課題.png

 使用した関数
  fputs
fputs(ファイルポインタ,データ);
   ファイルにデータを保存することができます。ファイルに書き出すとき
   注意したいのはアクセスモードです。書き出しのアクセスモードには2種類あります。
   
   'w':上書きモード
   'a':追記モードになる

インターネット上のHTMLソースを読み込む

index.phpを以下のように編集

<?php

 $url = '';
 $result ='';
 if($_POST !=null){
     $url = $_POST['text1'];
     $lines =file($url);
     $result =implode($lines);
 }
?>

<!DOCTYPE html 
          PUBLIC" -//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="ja" lang="ja">
<head>
    <meta http-equiv="Content=Type" content="text/html; charset=UTF-8"/>
    <title>sample page</title>
</head>
<body>
      <h1>hello PHP!</h1>
    <form method="post" action="./index.php">
     <input type="text" name="text1" size="40" value="<?php echo htmlspecialchars($url);?>"><br>
     <input type="submit">
    </form>
    <hr>
    <?php echo htmlspecialchars($result); ?>
</body>      
</html>

 googlechromeで「https://localhost/sample/index.php」にアクセス後
 入力欄にURLを入力して送信ボタンを押すその後htmlソースコードが表示されることを確認する
 image.png

所感

 
 今回の勉学でfopen、fputs、fclose関数の使い方をしれました。これで簡単なテキストファイルの
 読み込みや上書き等のプログラム等作成できるようになり大変良かったと思っております。
 また、file関数でURLを指定すると指定されたHTMLソースを読み込むがびっくりしました。
 明日は日時の操作について実施いたいます。

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0