第11週 讀寫檔案 留言版 上載檔案 <?php // 檔案操作 $fp=fopen("test.txt","w"); // 開啟檔案 寫入 for ($i=1; $i<=100; $i++) { fputs($fp,$i); // 寫入檔案 fputs($fp,"\n"); // 跳列 } fclose($fp); // 關檔 $fp=fopen("test.txt","r"); // 開檔 輸入 while ($line=fgets($fp)) { // 讀入檔案,每次一列 echo $line; // 顯示於瀏覽器 echo "<br>"; // 跳列 }; fclose($fp); // 關檔 ?> <?php // 留言板 $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post action=$SELF_PHP> 留言版: <p> <textarea name='note' rows=3 cols=40 wrap=physical> </textarea> <p> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $note=$_POST["note"]; $note = str_replace("\n", "<br>", $note); echo("<html> <body> <center> 留言: <p> $note </center> </body> </html>"); } ?>
<?php 檔案上傳 $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post enctype=multipart/form-data action=$SELF_PHP> 選取檔案: <input type=file name=filename> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $filename=$_FILES["filename"]["name"]; $tempname=$_FILES["filename"]["tmp_name"]; unlink($filename); if(copy($tempname,$filename)==1) echo "檔案上傳OK"; else echo "檔案上傳失敗"; } ?> 作業:您的作業有兩個選擇 1. 單獨作以上三個作業:開檔、寫檔、讀檔(完成三個70分) 2. 整合以上三個範例:先做一個留言版,將留言寫到一個檔案、 再將檔岸上傳至ccs.cust.edu.tw(完成100分) |