《php基礎教程》——4檔案和目錄 學習總結

來源:互聯網
上載者:User

標籤:php基礎教程   php檔案和目錄 學習總結   php檔案使用   

一、寫入檔案:
$fp = fopen(filename, mode);
  mode 為開啟檔案方式比如 ‘a+’表示讀寫檔案,檔案不存在則建立,新資料主加到檔案尾部

fwrite($fp,寫入內容);

fclose($fp);

二、鎖定檔案:當有多個使用者同時對檔案操作時保證不出現衝突

        fclock($fp, 鎖定類型); //鎖定類型包括寫入獨享、讀取共用……

三、讀取檔案:

$data = file(‘檔案路徑‘);

將讀取的資料放到data數組內,每個元素為檔案的一行。

編碼測試:ws.php:

<!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>test</title><base> </head> <body> <?phpif (isset($_POST['submitted'])) {if (!empty($_POST['quote']) && ($_POST['quote'] != "enter your quotation here")){//Write the data to file.$fp = fopen('../test.txt','a+');flock($fp, LOCK_EX);// lock the filefwrite($fp, $_POST['quote']);flock($fp, LOCK_UN);// unlockfclose($fp);//read the file's content to an array $data:$data = file('../test.txt');print '<p style = "color: red;"> data[0]:\n ' .$data[0]. '</p>';}else{print 'please enter your quotation';}}?><form action='ws.php' method='POST'><textarea name="quote" rows="5" cols="30">enter your quotation here</textarea><input type="submit" name="submit" value="Add this code"/><input type="hidden" name="submitted" value="true"/></form><div><p>This is the foot of the document</p></div></body> </html> 

測試裁圖:

起始:


結果:




五、處理檔案上傳:

1.form標籤必須包含:enctype="multipart/form-data",表單必須用post方法

2.必須添加一個隱藏輸入框,建議瀏覽器最大上傳檔案大小(以位元組計)

3.建立表單欄位<input type="file" name="thefile" />

代碼測試:ws.php:

<!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>test</title><base> </head> <body> <?phpif (isset($_POST['submitted'])) {if (move_uploaded_file($_FILES['thefile']['tmp_name'],"../uploads/{$_FILES['thefile']['name']}")){print '<p>your file is upload!</P>';}else{$errors = $_FILES['thefile']['error'];print $errors;}}?><form action='ws.php' enctype="multipart/form-data" method='POST'><p>Upload a file</p><input type="hidden" name="MAX_FILE_SIZE" value="3000"/><p><input type="file" name="thefile" /></p><input type="submit" name="submit" value="UPload"/><input type="hidden" name="submitted" value="true"/></form><div><p>This is the foot of the document</p></div></body> </html> 

六、目錄

1.導航目錄:

尋找目錄中所有內容:scandir();

eg: $stuff = scandir(‘目錄名‘);//函數返回一個數組,包含目錄中的檔案和目錄,複製給$stuff

2.建立目錄:

mkdir( ‘路徑名‘ );


《php基礎教程》——4檔案和目錄 學習總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.