php學(xué)習(xí)基礎(chǔ)-文件系統(tǒng)(2) 文件讀寫操作、文件資源處理
Jun 13, 2016 am 11:56 AM
php學(xué)習(xí)基礎(chǔ)-文件系統(tǒng)(二) 文件讀寫操作、文件資源處理
一、文件的打開與關(guān)閉
/* *讀取文件中的內(nèi)容 * file_get_contents(); //php5以上 * file() * readfile(); * * 不足:全部讀取, 不能讀取部分,也不能指定的區(qū)域 * * fopen() * fread() * fgetc() * fgets() * * * * * 寫入文件 * file_put_contents(“URL”, “內(nèi)容字符串”); //php5以上 * 如果文件不存在,則創(chuàng)建,并寫入內(nèi)容 * 如果文件存在,則刪除文件中的內(nèi)容,重新寫放 * * 不足: 不能以追加的方式寫,也不能加鎖 * * fopen() * fwrite() 別名 fputs * * * 本地文件: * ./test.txt * c:/appserv/www/index.html * /usr/local/apahce/index.html * * 遠(yuǎn)程: * 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 class="brush:php;toolbar:false">';print_r($sqls);echo '';
二、小偷程序,抓取網(wǎng)站上的頁面,從頁面鏈接中獲取資源圖片
$str=file_get_contents("http://www.163.com");preg_match_all('/\<img alt="php學(xué)習(xí)基礎(chǔ)-文件系統(tǒng)(2) 文件讀寫操作、文件資源處理" >/i',$str, $images);$imgs=""; foreach($images[0] as $img){ $imgs.=$img.'<br>';}echo file_put_contents("test.txt", $imgs);
三、更改網(wǎng)站配置項(xiàng),修改文件內(nèi)容,先讀取,在使用正則匹配寫入
if(isset($_POST["sub"])){ setConfig($_POST);}function setConfig($post){ //讀取文件中的內(nèi)容 $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 class="brush:php;toolbar:false">'; print_r($zz); print_r($rep); echo ''; //改寫文件中的內(nèi)容 $str=preg_replace($zz, $rep, $str); file_put_contents("config.inc.php", $str); //再寫回文件}?>
四、讀取文件中得sql,執(zhí)行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 class="brush:php;toolbar:false">';print_r($sqls);echo '';
五、向文件中寫入內(nèi)容
/*寫入文件 * file_put_contents(“URL”, “內(nèi)容字符串”); //php5以上 * 如果文件不存在,則創(chuàng)建,并寫入內(nèi)容 * 如果文件存在,則刪除文件中的內(nèi)容,重新寫放 * * 不足: 不能以追加的方式寫,也不能加鎖 * * fopen() * fwrite() 別名 fputs */ $file=fopen("./test.txt", "a"); //如果打開文件成功返回資源,如果失敗返回false for($i=0; $i<br><br><p>六、循環(huán)讀取文件每次按照固定長度讀取</p><p></p><pre code_snippet_id="324578" snippet_file_name="blog_20140503_6_8088416" name="code" class="php"> $file=fopen("http://www.163.com", "r"); //如果打開文件成功返回資源,如果失敗返回false $str=""; while(!feof($file)){ $str.=fread($file, 1024); } echo $str; fclose($file); //關(guān)閉文件資源
七、文件常用函數(shù)實(shí)例
$file=fopen("./test.txt", "r"); //如果打開文件成功返回資源,如果失敗返回false echo 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); //關(guān)閉文件資源

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties
