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>.';
}
}
}
?>