PHP Learning Basics-File System (2) file read and write operations, file resource processing

Source: Internet
Author: User
Tags fread rtrim
PHP Learning Basics-File System (ii) file read and write operations, file resource processing

I. Opening and closing of files

/* * Read the contents of the file *file_get_contents (); PHP5 above *file () *readfile (); * * Insufficient: Read all, cannot read the section, also cannot specify the area * *fopen () *fread () *fgetc () *fgets () * * * * * * Write file *file_put_contents ("URL", "content string");  PHP5 above * If the file does not exist, then create, and write to the content * If the file exists, delete the contents of the file, re-write * Insufficient: Can not be written in addition to, nor lock *  *fopen () *fwrite () alias fputs * * * Local file: *./ Test.txt *c:/appserv/www/index.html */usr/local/apahce/index.html * * Remote: *http://www.baidu.com *http://www.163.com * * Ftp:[email protected]:www.baidu.com/index.php * *///reads all rows $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 '
';p rint_r ($SQLS); Echo '
';



Second, the Thief program, crawl pages on the site, from the page link to get the resource picture

$str =file_get_contents ("http://www.163.com");p reg_match_all ('/\/i ', $str, $images); $imgs = ""; foreach ($images [0] As $IMG) {$imgs. = $img. '
';} Echo file_put_contents ("Test.txt", $imgs);

Third, change the site configuration items, modify the contents of the file, read first, in the use of regular matching write

if (Isset ($_post["sub"])) {setconfig ($_post);} function Setconfig ($post) {//reads the contents of the file $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 '
';p rint_r ($zz);p rint_r ($rep); Echo '
';//rewrite the contents of the file $str=preg_replace ($zz, $rep, $str); File_put_contents ("config.inc.php", $str);//write back the file}?>

IV. read SQL in file, execute 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 '
';p rint_r ($SQLS); Echo '

V. Writing content to a file

   /* Write    to file *file_put_contents ("URL", "content string");  PHP5 above    * If the file does not exist, then create and write to the content    * If the file exists, delete the contents of the file, re-write    *    insufficient: Can not be written in an additional way, nor lock    *    *fopen ()    *fwrite () alias fputs    */    $file =fopen ("./test.txt", "a");//If the open file successfully returns a resource, if the failure returns false for    ($i =0; $i <100; $i + +) fwrite ($file, "www.lampbrother{$i}.net\n");    Fclose ($file);   Close File Resource


Six, the loop reads the file each time according to fixed length reads

    $file =fopen ("http://www.163.com", "R"); If the open file successfully returns a resource, if the failure returns false    $str = "";    while (!feof ($file)) {$str. =fread ($file, 1024x768);    }    echo $str;    Fclose ($file);   Close File Resource


Vii. examples of common functions of files

   $file =fopen ("./test.txt", "R"); If the open file successfully returns a resource, if the failure returns Falseecho Ftell ($file).
"; Echo fread ($file, 10)."
"; Echo Ftell ($file)."
"; Echo fread ($file, 10)."
"; Echo Ftell ($file)."
"; Fseek ($file, seek_cur); Echo Ftell ($file)."
"; Echo fread ($file, 10)."
"; Echo Ftell ($file)."
"; Fseek ($file, -20, seek_end); Echo fread ($file, 20)."
"; Echo Ftell ($file)."
"; Rewind ($file); Go back to the head of the file echo Ftell ($file).
"; Echo fread ($file, 20)."
"; fclose ($file); Close File Resource



  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.