9.PHP文件处理
PHP文件系統
(當成是擴展C++來看就行了,幾乎一樣):
讀取整個文件readfile()?、file()、?file_get_contents()
<?php
????readfile('file.dat');
????echo?'<br>';
????$f_arr?=?file('file.dat');
????foreach($f_arr?as?$cont){
????????echo?$cont."<br>";
????}
????echo?'<br>';
????$f_chr?=?file_get_contents('file.dat');
????echo?$f_chr;
?>
讀取一個字符就用???string?fgetc(resource?handle)
讀取制定長度的字符?string?fread(resource?handle?,int?length)
獲取一行數據fgets()?fgetss()
<?php
???$fopen?=?fopen('file.dat'?,'rb');
????while(!feof($fopen)){
????????echo?fgets($fopen);
????}
????fclose($fopen);
????echo'<br>..................<br>';
????$fopen2?=?fopen('file.dat'?,'rb');
????while(!feof($fopen2)){
????echo?fgetss($fopen2);
????}
????fclose($fopen2);
?>
將數據寫入文件:fwrite()??file_put_contents()
<?php
????$filepath?=?"w.txt";
????$str?=?"ci?qing?ke?dai?cheng?zhui?yi?,zhi?shi?dang?shi?yi?wang?ran<br>";
????$fopen?=?fopen($filepath?,'wb')?or?dir('wen?jian?bu?cun?zai');
????fwrite($fopen?,$str);
????fclose($fopen);
????readfile($filepath);
????file_put_contents($filepath?,$str);
????readfile($filepath);
?>
文件操作:
目錄處理
枚舉目錄:
<?php
????$path?=?'C:';
????if(is_dir($path)){
????????$dir?=?scandir($path);
????????foreach($dir?as?$value){
????????????echo?$value."<br>";
????????}
????}else{
????????echo?'error';
????}
?>
?
目錄操作:
?
遠程文件訪問:
文件指針?rewind()?fseek()?ftell()
文件鎖定:
?
文件上傳:
?
?
?
<table?width="500"?border="0"?cellspacing="0"?cellpadding="0">
????<form?action=""?method="post"?enctype="multipart/form-data">
????????<tr>
????????????<td?width="150"?height="30"?align="right"?valign="middle">Wen?Jian:</td>
????????????<td?width="250"><input?type="file"?name="upfile"/></td>
????????????<td?width="100"><input?type="submit"?name="submit"?value="shang?chuan"/></td>
????????</tr>
????</form>
</table>
<?php
if(!empty($_FILES['upfile']['name'])){
//????foreach($_FILES['upfile']?as?$name?=>?$value){
//????????echo?$name.'='.$value.'<br>';
//????}
????$fileinfo?=?$_FILES['upfile'];
????if($fileinfo['size']?<?10000000?&&?$fileinfo['size']?>?0){
????????move_uploaded_file($fileinfo['tmp_name']?,$fileinfo['name']);
????????echo?'yes';
????}else{
????????echo?'no';
????}
}
?>
文件批量上傳
<form?action=""?method="post"?enctype="multipart/form-data">
????<table?id="up_table"?border="1"?bgcolor="f0f0f0">
????????<tbody?id="auto">
????????????<tr?id="show">
????????????????<td>shang?chuan</td>
????????????????<td><input?name="u_file[]"?type="file"></td>
????????????</tr>
????????????<tr>
????????????????<td>shang?chuan</td>
????????????????<td><input?name="u_file[]"?type="file"></td>
????????????</tr>
????????</tbody>
????????<tr><td?colspan="4"><input?type="submit"?value="shang?chuan"/></td></tr>
????</table>
</form>
<?php
if(!empty($_FILES['u_file']['name'])){
????$file_name?=?$_FILES['u_file']['name'];
????$file_tmp_name?=?$_FILES['u_file']['tmp_name'];
????for($i?=?0?;$i?<?count($file_tmp_name)?;$i?++){
????????if($file_name[$i]?!=?""){
????????????move_uploaded_file($file_tmp_name[$i]?,$i.$file_name[$i]);
????????????echo?'wen?jian'.$file_name[$i].'yes<br>.';
????????}
????}
}
?>
?
?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: 8.PHP图像处理
- 下一篇: 10.PHP加密相关