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 ' <pre> ';p rint_r ($SQLS); Echo ' </pre> ';
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. ' <br> ';} 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 ' <pre> ';p rint_r ($zz);p Rint_r ($rep), echo ' </pre> ';//rewrite the contents of the file $str=preg_replace ($zz, $rep, $STR); File_put_contents ("config.inc.php", $str);//write back the file}?><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>
IV. read SQL in file, execute SQL
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). <br> "; Echo fread ($file, 10)." <br> "; Echo Ftell ($file)." <br> "; Echo fread ($file, 10)." <br> "; Echo Ftell ($file)." <br> "; fseek ($file, 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); Go back to the head of the file echo Ftell ($file). <br> "; Echo fread ($file, 20)." <br> "; fclose ($file); Close File Resource