php學習基礎-檔案系統(二) 檔案讀寫操作、檔案資源處理

來源:互聯網
上載者:User

標籤:blog   class   code   ext   get   int   

一、檔案的開啟與關閉

/* *讀取檔案中的內容 *file_get_contents(); //php5以上 *file() *readfile(); * *不足:全部讀取, 不能讀取部分,也不能指定的地區 * *fopen() *fread() *fgetc() *fgets() * * * * *寫入檔案 *file_put_contents(“URL”, “內容字串”);  //php5以上 *如果檔案不存在,則建立,並寫入內容 *如果檔案存在,則刪除檔案中的內容,重新寫放 * *不足: 不能以追加的方式寫,也不能加鎖 *  *fopen() *fwrite() 別名 fputs * * *本地檔案: *./test.txt *c:/appserv/www/index.html */usr/local/apahce/index.html * *遠程: *http://www.baidu.com *http://www.163.com * * ftp://[email protected]:www.baidu.com/index.php * *///讀取出所有行$lines=file("lampcms.sql");$sqlstr="";foreach($lines as $line){$line=trim($line);if($line!=""){if(!($line{0}=="#" || $line{0}.$line{1}=="--")){$sqlstr.=$line;}}}$sqlstr=rtrim($sqlstr,";");$sqls=explode(";",$sqlstr);echo ‘<pre>‘;print_r($sqls);echo ‘</pre>‘;



二、小偷程式,抓取網站上的頁面,從頁面連結中擷取資源圖片

$str=file_get_contents("http://www.163.com");preg_match_all(‘/\<img\s+src=.*?\s*\/?\>/i‘,$str, $images);$imgs="";foreach($images[0] as $img){$imgs.=$img.‘<br>‘;}echo file_put_contents("test.txt", $imgs);

三、更改網站配置項,修改檔案內容,先讀取,在使用正則匹配寫入

if(isset($_POST["sub"])){setConfig($_POST);}function setConfig($post){//讀取檔案中的內容$str=file_get_contents("config.inc.php");$zz=array();$rep=array();foreach($post as $key=>$value ){$zz[]="/define\(\"{$key}\",\s*.*?\);/i";$rep[]="define(\"{$key}\", \"{$value}\");";}echo ‘<pre>‘;print_r($zz);print_r($rep);echo ‘</pre>‘;//改寫檔案中的內容$str=preg_replace($zz, $rep, $str);file_put_contents("config.inc.php", $str);//再寫迴文件}?><form action="one.php" method="post">host : <input type="text" name="DB_HOST"><br>user:  <input type="text" name="DB_USER"><br>pass: <input type="text" name="DB_PWD"><br>dbname <input type="text" name="DB_NAME"><br>tabPREFIX <input type="text" name="TAB_PREFIX"><br><input type="submit" name="sub" value="mod"><br></form>

四、讀取檔案中得sql,執行sql

$lines=file("lampcms.sql");$sqlstr="";foreach($lines as $line){$line=trim($line);if($line!=""){if(!($line{0}=="#" || $line{0}.$line{1}=="--")){$sqlstr.=$line;}}}$sqlstr=rtrim($sqlstr,";");$sqls=explode(";",$sqlstr);echo ‘<pre>‘;print_r($sqls);echo ‘</pre>‘; 

五、向檔案中寫入內容

   /*寫入檔案    *file_put_contents(“URL”, “內容字串”);  //php5以上    *如果檔案不存在,則建立,並寫入內容    *如果檔案存在,則刪除檔案中的內容,重新寫放    *    *不足: 不能以追加的方式寫,也不能加鎖    *    *fopen()    *fwrite() 別名 fputs    */    $file=fopen("./test.txt", "a"); //如果開啟檔案成功返回資源,如果失敗返回false    for($i=0; $i<100; $i++)fwrite($file, "www.lampbrother{$i}.net\n");    fclose($file);   //關閉檔案資源


六、迴圈讀取檔案每次按照固定長度讀取

    $file=fopen("http://www.163.com", "r"); //如果開啟檔案成功返回資源,如果失敗返回false    $str="";    while(!feof($file)){$str.=fread($file, 1024);    }    echo $str;    fclose($file);   //關閉檔案資源


七、檔案常用函數執行個體

   $file=fopen("./test.txt", "r"); //如果開啟檔案成功返回資源,如果失敗返回falseecho ftell($file)."<br>";echo fread($file, 10)."<br>";echo ftell($file)."<br>";echo fread($file, 10)."<br>";echo ftell($file)."<br>";fseek($file,100, SEEK_CUR);echo ftell($file)."<br>";echo fread($file, 10)."<br>";echo ftell($file)."<br>";fseek($file,-20, SEEK_END);echo fread($file, 20)."<br>";echo ftell($file)."<br>";rewind($file); //回到檔案頭部echo ftell($file)."<br>";echo fread($file, 20)."<br>";fclose($file);   //關閉檔案資源



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.