PHP學習文件處理與文件上傳 課件第1/2頁
Jun 13, 2016 pm 12:28 PM
????PHP對文件的處理
PHP?在服務器端文件的應用上,相關的范疇不僅僅在于用戶與服務器資料庫間的各種連接存取操作,也可以通過?PHP?內(nèi)建的文件處理函數(shù),來進行一般文件處理操作。
basename?--?返回路徑中的文件名部分
語法格式:$path?=?"/home/httpd/html/index.php";
??$file?=?basename($path);????????//?$file?is?set?to?"index.php"
??$file?=?basename($path,".php");?//?$file?is?set?to?"index"
pathinfo():分析文件當前路徑
語法格式:$path_parts?=?pathinfo("/www/htdocs/index.html");
??echo?$path_parts["dirname"]?.?"\n";??/www/htdocs??文件路徑
??echo?$path_parts["basename"]?.?"\n";??index.html??文件與擴展名
??echo?$path_parts["extension"]?.?"\n";??html????文件格式
????文件類型與相關信息
filesize():計算文件的大小(byte)
語法格式:$bytes=filesize(“a.txt”);
??echo?$bytes?????round($bytes/1024,2);
fileatime():文件最后一次訪問時間(時間戳)
語法格式:echo?date(“Y-m-d?g:i:sa”,fileatime);
filectime():文件建立時間
語法格式:echo?date(“Y-m-d?g:i:sa”,filectime);
filemtime():文件最后一次更新時間
語法格式:echo?date(“Y-m-d?g:i:sa”,filemtime);
fileperms():文件屬性以及權限10進制
語法格式:echo?substr(base_convert(fileperms(a.txt),10,8),3);
fileowner():文件所有者的uid(僅在Linux系統(tǒng)下有用)
語法格式:echo?fileowner(“a.txt”);
????文件的操作
'r'?只讀方式打開,將文件指針指向文件頭。??
'r+'?讀寫方式打開,將文件指針指向文件頭。??
'w'?寫入方式打開,將文件指針指向文件頭并將文件大小截為零。如果文件不存在則嘗試創(chuàng)建之。??
'w+'?讀寫方式打開,將文件指針指向文件頭并將文件大小截為零。如果文件不存在則嘗試創(chuàng)建之。??
'a'?寫入方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創(chuàng)建之。??
'a+'?讀寫方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創(chuàng)建之。??
'x'???創(chuàng)建并以寫入方式打開,將文件指針指向文件頭。如果文件已存在,則?fopen()?調(diào)用失敗并返回?FALSE,并生成一條?E_WARNING?級別的錯誤信息。如果文件不存在則嘗試創(chuàng)建之。這和給?底層的?open(2)?系統(tǒng)調(diào)用指定?O_EXCL|O_CREAT?標記是等價的。此選項被?PHP?4.3.2?以及以后的版本所支持,僅能用于本地文件。??
'x+'???創(chuàng)建并以讀寫方式打開,將文件指針指向文件頭。如果文件已存在,則?fopen()?調(diào)用失敗并返回?FALSE,并生成一條?E_WARNING?級別的錯誤信息。如果文件不存在則嘗試創(chuàng)建之。這和給?底層的?open(2)?系統(tǒng)調(diào)用指定?O_EXCL|O_CREAT?標記是等價的。此選項被?PHP?4.3.2?以及以后的版本所支持,僅能用于本地文件。??
fopen()?--?打開文件或者?URL??
fclose()?--?關閉一個已打開的文件指針
fread()?--?讀取文件內(nèi)容
fwrite()--寫入文件
語法格式:if(!$f=@fopen("file03.php","x")){//打開一個文件?x方法寫入
????die("文件讀取失敗");????//讀取失敗
??}
??fwrite($f,"kkkkkkkkkk");??//向那個文件寫??寫什么
??echo?fread($f,10);????//讀取這個文件的內(nèi)容
??fclose($f);??????關閉打開文件
file()?--?把整個文件讀入一個數(shù)組中
語法格式:$arr=file("file03.php");
??print_r($arr);?//讀取文件返回數(shù)組
readfile()?--讀入一個文件并寫入到輸出緩沖。?
語法格式:$str=readfile("file03.php");
??echo?$str;??輸出
一個文件記數(shù)器的寫法:
$f=fopen("file03.php","r");
??$i=fread($f,1000);
??echo?"這是您第{$i}次訪問";
??fclose($f);
??$f=fopen("file03.php","w");??
??$i++;
??fwrite($f,$i);
??fclose($f);
````````````````````````````````````````
file_get_contents()?–?將整個文件讀入一個字符串
語法格式:file_get_contents(文件名稱或URL)
file_put_contents()?–?將一個字符串寫入文件,和依次調(diào)用?fopen(),fwrite()?以及?fclose()?功能一樣
file_put_contents(文件名稱,寫入數(shù)據(jù))
feof()?--?測試文件指針是否到了文件結束的位置
ftell()?--?返回文件指針讀/寫的位置
語法格式ftell(文件指針)
flock()?--?輕便的咨詢文件鎖定
語法格式:flock(文件指針,控制參數(shù))
文件指針:是一個已經(jīng)打開(fopen)的文件指針控制參數(shù):
“LOCK_SH”表示要取得共享鎖定(讀取程序),(PHP4.0.1以前版本設置1)。?
“LOCK_EX”表示要取得獨占鎖定(寫入程序),(PHP4.0.1以前版本中設置為?2)。?
“LOCK_UN”表示要釋放鎖定(無論共享或獨占),(PHP4.0.1以前版本中設置為?3)。?
“LOCK_NB”表示如果你不希望?flock()?在鎖定時堵塞,則給控制參數(shù)再加上這個參數(shù)
fseek(?)?--?在文件指針中定位
語法結構:??fseek(文件指針,移動字元數(shù)?[,起始位置常數(shù)])
文件指針:不能用于在?fopen()?中以?“http://”?或?“ftp://”?格式打開所返回的文件指針。
移動字元數(shù):為正數(shù)時,將文件指針向前移動指定個數(shù):為負數(shù)時,將文件指針向后移動指定個數(shù):
起始位置常數(shù):?
SEEK_CUR?-?設定位置為當前位置。?
SEEK_SET?-?設定位置等于文件開頭。(默認值)
SEEK_END?-?設定位置為文件尾。
rewind(?)?--?倒回文件指針的位置,也就是將文件指針移向文件的開頭位置。?
語法結構:??rewind(文件指針)
注意:如果將文件以附加(“a”?或者?“a+”)模式打開,寫入文件的任何數(shù)據(jù)總是會被附加在后面,不管文件指針的位置
chgrp(?)?--?改變文件所屬的組
??語法結構:chgrp(文件名稱,群組名稱)
filegroup(?)?--?取得文件的組
??語法結構:filegroup(文件名稱)
chmod(?)?--?改變文件模式
??語法結構:chmod(文件名稱,權限常數(shù))755??666
chown(?)?--?改變文件的所有者
??語法結構:chown(文件名稱,使用者)
fileowner(?)?--?取得文件的所有者
??語法結構:fileowner(文件名稱)
posix_getpwuid()?來將其解析為用戶名。
??copy(?)?--?拷貝文件
??語法結構:copy(來源文件,目的文件)
??返回類型:bool型,如果成功則返回?TRUE,失敗則返回?FALSE。
??參數(shù)說明:將來源文件拷貝到目的文件。
??unlink(?)?--?刪除文件
??語法結構:unlink(目標文件)
??返回類型:bool型,如果成功則返回?TRUE,失敗則返回?FALSE。
??參數(shù)說明:刪除指定的目標文件
??rename(?)?--?重命名一個文件或目錄
??語法結構:rename(舊文件名,新文件名)
??返回類型:bool型,如果成功則返回?TRUE,失敗則返回?FALSE。
??參數(shù)說明:嘗試把舊文件名重命名為新文件名。
文件屬性處理
file_exists(?)?--?檢查文件或目錄是否存在
??語法結構:file_exists(文件名稱)
??返回類型:bool型,若存在返回?true,否則返回?false。
filesize(?)?--?取得文件大小
??語法結構:filesize(文件名稱)
??返回類型:返回文件大小字節(jié)數(shù),若出錯返回?false.
filetype(?)?--?取得文件類型
???語法結構:filetype(文件名稱)
???返回類型:返回文件類型??赡艿闹涤?fifo,char,dir,block,link,file?和?unknown。?出錯則返回?false
is_dir(?)?--?判斷給定文件名是否是一個目錄
??語法結構:is_dir(名稱)
??返回類型:如果文件名存在并且是一個目錄則返回?true,否則返回?false。
is_executable(?)?--?判斷給定文件名是否可執(zhí)行
???語法結構:is_executable(名稱)
????返回類型:如果文件存在且可執(zhí)行則返回?TRUE,否則返回?FALSE。??
is_file(?)?--?判斷給定文件名是否為一個正常的文件
??語法結構:is_file(名稱)??
??返回類型:如果文件存在且為正常的文件則返回?TRUE。
is_link(?)?--?判斷給定文件名是否為一個符號連接
??語法結構:is_link(名稱)???
??返回類型:如果文件存在并且是一個符號連接則返回?true。
is_readable(?)?--?判斷給定文件名是否可讀
??語法結構:is_readable(文件名稱)??
??返回類型:如果文件存在并且可讀則返回?TRUE。
is_writable(?)?--?判斷給定的文件名是否可寫
??語法結構:is_writable(文件名稱)??
??返回類型:如果文件存在并且可寫則返回?TRUE。
????實現(xiàn)iterator接口的目錄讀取
Iterator接口的標準方法
current():返回當前列表(list)中的元素值。
next():用于在一個列表中向下移動一個位置。
valid():檢測在當前列表中是否還有下一個元素,如果有,返回true,否則返回false。
rewind():可以訪問指定特征的元素列表,在開始操作iterator時,會將指針設置在頂部。

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
