テキストファイルの読み込みについて - PHPプロ!Q&A掲示板
2863
- 0P
- 0P
テキストファイルの読み込みについて
質問日時 / 2010年9月11日 05:00 (最終編集:9月11日 08:19) 回答数 / 10件
Questioner: reika_ciruela
Tweet
fgets()やfile_get_contents()での読み込みを試してみましたが、エラーが出てできません。
独学で勉強中の素人のものなので恥かしいですが、以下がソースです。
どなたかアドバイスお願いします。
■fgets()のエラー
Warning: fopen(testlog.txt) [function.fopen]: failed to open stream: No such file or directory in C:\xampp\htdocs\test\convert.php on line 60
Warning: feof() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\convert.php on line 61
Warning: fgets() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\convert.php on line 62
以下 line 61 と line 62 のエラーが永遠と繰り返されます
■file_get_contents()のエラー
Warning: file_get_contents(testlog.txt) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\test\convert.php on line 60
■テスト用txtファイル(testlog.txt)
- [2010/08/18 23:43:14] ハンドルネーム: 発言内容
- [2010/08/18 23:43:22] ハンドルネーム: 発言内容
- [2010/08/18 23:43:22] ハンドルネーム: 発言内容
- [2010/08/19 21:23:50] ハンドルネーム: 発言内容
- [2010/08/19 21:24:07] ハンドルネーム: 発言内容
- [2010/08/20 21:25:03] ハンドルネーム: 発言内容
■fgets()版phpファイル(convert.php)
- if (isset($_POST['file']) == FALSE){
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>チャットログ(Skype)変換ツール</title>
- <style type=text/css>
- <!--
- body {
- background: #fffff;
- font-size: 90%;
- font-family:;
- }
- td.info {
- font-size: 12px;
- }
- h1 {
- font-size: 120%;
- text-align: center;
- }
- -->
- </style>
- </head>
- <body>
- <h1>txt→html<br />チャットログ(Skype)変換ツール</h1>
- <form action=convert.php method=POST >
- <table width=100% cellspacing=12><tr align=center>
- <td><table border=0 width=480>
- <tr align=left>
- <td>タイトル:</td>
- <td><input type=text name=title /></td>
- <td class=info>※チャットのテーマタイトルをどうぞ。<br /> </td>
- </tr>
- <tr align=left>
- <td>ファイル:</td>
- <td><input type=file name=file /></td>
- <td class=info>※htmlファイルに変換したい<br /> txtファイルを選択してください。</td>
- </tr>
- </table></td>
- </tr>
- <tr align="center">
- <td><input type=submit value=変換 /></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- } else {
- $ftitle = $_POST['title'];
- $fdata = $_POST['file'];
- $pathinfo = pathinfo($fdata);
- $fname = $pathinfo['filename'];
- header("Content-Disposition: attachment; filename= $fname.html");
- mb_internal_encoding("UTF-8");
- /* ファイルの読み込み */
- $fp = fopen($fdata,'rb');
- while (feof($fp) == FALSE) {
- $str = fgets($fp);
- $str = mb_convert_encoding($str, "UTF-8", "Shift_JIS, EUC-JP, UTF-8");
- }
- fclose($fp);
- /* ファイルの読み込み終わり */
- preg_match_all("/\d{4}\/\d{2}\/\d{2}/",$str,$dateArry);
- $date = array_unique($dateArry[0]);
- /* 置換スタート */
- $rekakko1 = preg_replace("/\[/",'</td></tr><tr valign="top"><td align="center" width="20">',$str);
- $redate = preg_replace("/\d{4}\/\d{2}\/\d{2}\s(?!\d{2}\:\d{2]\:\d{2})/",'[',$rekakko1);
- $rekakko2 = preg_replace("/\]\s/",']</td><td align="right" width="140">',$redate);
- $result = preg_replace("/\:\s/",':</td><td>',$rekakko2);
- /* 置換作業完了 */
- if (count($date) >= 2){
- $datetitle = '※' . reset($date) . '~' . end($date) . 'のチャットログです。';
- } else {
- $datetitle = '※' . reset($date) . 'のチャットログです。';
- }
- echo '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>' . $ftitle . '</title>
- <link rel="shortcut icon" href="../img/favicon.ico" type="image/vnd.microsoft.icon">
- <style type="text/css">
- <!--
- body,td,th {
- color: #000000;
- }
- body {
- background-color: #FFFFFF;
- margin: 10px;
- font-family:;
- }
- -->
- </style>
- <body>
- <h1>【' . $ftitle .'】</h1>'
- . $datetitle .'<br /><hr>
- <table width="99%"><tr valign="top">'
- . $result
- . '</td></tr></table></body></html>';
- }
■file_get_contents()の場合
59. /* ファイルの読み込み */
60. $str = file_get_contents($filename);
61. $str = mb_convert_encoding($str, "UTF-8", "Shift_JIS, EUC-JP, UTF-8");
62. /* ファイルの読み込み終わり */





ページのトップへ


GETのままでは検索エンジンのロボットが拾ってくれなかったためにSEO対策として有効だと言われていますね。