php學(xué)習(xí)基礎(chǔ)-文件系統(tǒng)(1) 文件處理,文件權(quán)限
Jun 13, 2016 am 11:56 AM
php學(xué)習(xí)基礎(chǔ)-文件系統(tǒng)(一) 文件處理,文件權(quán)限
/* PHP文件系統(tǒng)處理 * 所有文件處理都是使用系統(tǒng)函數(shù)完成的。 * 是基于Linux/Unix系統(tǒng)為模型 * * 文件系統(tǒng)處理的作用: * 1. 所有的項(xiàng)目離不開文件處理 * 2. 可以用文件長時(shí)間保存數(shù)據(jù) * 3. 建立緩存, 服務(wù)器中文件操作 * * 文件處理 * 1. 文件類型 * 以Linux為模型的, 在Windows只能獲取file, dir或unknow 三種類型 * 在Linux/Unix下, block, char, dir, fifo, file, link, unknown和種型 * block :塊設(shè)置文件,磁盤分區(qū),軟驅(qū), cd-rom等 * char: 字符設(shè)備,I/O 以字符為單位, 鍵盤,打印機(jī)等 * dir: 目錄也是文件的一種 * fifo: * file: * link: * unknown * * filetype("目錄或文件名") * * is_array(); * is_int(); * is_string(); * is_null; * is_bool(); * is_dir -- 判斷給定文件名是否是一個(gè)目錄 is_executable -- 判斷給定文件名是否可執(zhí)行 is_file -- 判斷給定文件名是否為一個(gè)正常的文件 is_link -- 判斷給定文件名是否為一個(gè)符號(hào)連接 is_readable -- 判斷給定文件名是否可讀 is_uploaded_file -- 判斷文件是否是通過 HTTP POST 上傳的 is_writable -- 判斷給定的文件名是否可寫 is_writeable -- is_writable() 的別名 * * * 2. 文件的屬性 * file_exists(); * filesize(); * is_readable(); * is_writeable(); * filectime(); * filemtime(); * fileactime(); * stat(); * * 3. 和文件路徑相關(guān)的函數(shù) * * 相對路徑:相對于當(dāng)前目錄的上級(jí)和下級(jí)目錄 * . 當(dāng)前目錄 * .. 上一級(jí)目錄 * * ./php/apache/index.php * php/apahce/index.php * login.php * ./login.php * ../images/tpl/logo.gif * * * 路徑分隔符號(hào) * linux/Unix "/" * windows "\" * * DIRECTORY_SEPARATOR 為不同平臺(tái),在Windows \ Linux / * * 不管是什么操作系統(tǒng)PHP的目錄分割符號(hào)都支技 / (Linux) * * 在PHP和Apache配置文件中如果需要指定目錄,也使用/作為目錄符號(hào) * * 絕對路徑: * / 根路徑 * * /images/index.php * * 指的操作系統(tǒng)的根 * 指的是存放網(wǎng)站的文檔根目錄 * * 分情況 * * 如果是在服務(wù)器中執(zhí)行(通過PHP文件處理函數(shù)執(zhí)行)路徑 則 “根”指的就是操作系統(tǒng)的根 * 如果程序是下載的客戶端,再訪問服務(wù)器中的文件時(shí),只有通過Apache訪問,“根”也就指的是文檔根目錄 * * http://www.xsphp.com/logo.gif * * * basename(url) * dirname(url) * pathinfo(url) * * * * * 4. 文件的操作(創(chuàng)建文件,刪除文件,移動(dòng)文件) * 5. 文件的打開與關(guān)閉(讀文件中的內(nèi)容, 向文件中寫內(nèi)容) * 6. 文件內(nèi)部移動(dòng)指針 * 7. 文件的鎖定一些機(jī)制處理 * * * 目錄的處理 * 1. 目錄的遍歷 * 2. 目錄的創(chuàng)建 * 3. 目錄的刪除 * 4. 目錄的復(fù)制 * 5. 統(tǒng)計(jì)目錄大小 * * * 文件上傳和下載 * 1. 上傳 * 2. 下載 * * */
二、PHP文件屬性函數(shù)實(shí)例
date_default_timezone_set("PRC"); function getFilePro($fileName){ if(!file_exists($fileName)){ echo "文件或目錄{$fileName} 不存在<br>"; return; }else{ echo "文件的類型".filetype($fileName)."<br>"; } if(is_file($fileName)){ echo "這是一個(gè)文件<br>"; echo "文件的大小為".getFileSize(filesize($fileName))."<br>"; } if(is_dir($fileName)){ echo "這是一個(gè)目錄<br>"; } if(is_readable($fileName)){ echo "這個(gè)文件可以讀<br>"; } if(is_writable($fileName)){ echo "這個(gè)文件可以寫<br>"; } if(is_executable($fileName)){ echo "這個(gè)文件可以執(zhí)行<br>"; } echo "文件的創(chuàng)建時(shí)間:".date("Y-m-d H:i:s",filectime($fileName))."<br>"; echo "文件的修改時(shí)間:".date("Y-m-d H:i:s",filemtime($fileName))."<br>"; echo "文件的最后訪問時(shí)間:".date("Y-m-d H:i:s",fileatime($fileName))."<br>"; } function getFileSize($size){ $dw="Byte"; if($size >= pow(2, 40)){ $size=round($size/pow(2, 40), 2); $dw="TB"; }else if($size >= pow(2, 30)){ $size=round($size/pow(2, 30), 2); $dw="GB"; }else if($size >= pow(2, 20)){ $size=round($size/pow(2, 20), 2); $dw="MB"; }else if($size >= pow(2, 10)){ $size=round($size/pow(2, 10), 2); $dw="KB"; }else { $dw="Bytes"; } return $size.$dw; } getFilePro("demo.txt"); getFilePro("hello");
三、PHP獲取文件狀態(tài)函數(shù)
date_default_timezone_set("PRC"); echo '<pre class="brush:php;toolbar:false">'; print_r(stat("demo.txt")); echo '';
四、使用文件系統(tǒng)緩存數(shù)據(jù)方案
$cache=5; //緩存時(shí)間$cachefile="cache.txt"; //緩存的文件if(file_exists($cachefile) && (time()-$cache) <p><br></p><p>五、文件路徑相關(guān)函數(shù)實(shí)例</p><p></p><pre code_snippet_id="324388" snippet_file_name="blog_20140503_5_8122871" name="code" class="php">$url1="./aaa/bbb/index.php"; echo basename($url1)."<br>"; //文件名稱 echo dirname(dirname($url1))."<br>"; //父級(jí)目錄 echo dirname($url1)."<br>"; //文件目錄echo '<pre class="brush:php;toolbar:false">'; //文件路徑信息 print_r($path=pathinfo($url3)); echo ''; echo $path["extension"];
六、文件系統(tǒng)權(quán)限相關(guān)的函數(shù)實(shí)例
創(chuàng)建文件 touch("文件名") 刪除文件 unlink("文件路徑"); 移動(dòng)文件 為文件重新命名 rename("當(dāng)前文件路徑", “目錄為文件路徑”) 復(fù)制文件 copy("當(dāng)前", “目標(biāo)”); 一定要有PHP執(zhí)行這個(gè)文件權(quán)限, Apache, 一個(gè)用戶 和權(quán)限設(shè)計(jì)有關(guān)的函數(shù) ls -l 或 ll _rwxrwxrwx 777 _ 類型 _文件 d 表示是目錄 l b rwx 表這個(gè)文件的擁有者 r讀 w寫 x執(zhí)行 rwx 表這個(gè)文件的擁有者所在的組 r讀 w寫 x執(zhí)行 rwx 其它用戶對這個(gè)為文件的權(quán)限 r讀 w寫 x執(zhí)行 r 4 w 2 x 1 7 7 7 4+2+1 4+2+1 4+2+1 rwx rwx rwx 644 4+2 4 4 rw_ r__ r__ 754 chmod u=rwx,g=rw,o=x chmod 777 demo.php chmod 644 demo.html chown mysql demo.php chgrp apache demo.php chgrp -- 改變文件所屬的組 chmod -- 改變文件模式 chown -- 改變文件的所有者 filegroup -- 取得文件的組 fileowner -- 取得文件的所有者

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

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas
