天天看點

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;
?>      
9.PHP檔案處理

讀取一個字元就用   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);
?>      
9.PHP檔案處理

檔案操作:

9.PHP檔案處理

目錄處理

枚舉目錄:

<?php
    $path = 'C:';
    if(is_dir($path)){
        $dir = scandir($path);
        foreach($dir as $value){
            echo $value."<br>";
        }
    }else{
        echo 'error';
    }
?>      

目錄操作:

9.PHP檔案處理

遠端檔案通路:

9.PHP檔案處理

檔案指針 rewind() fseek() ftell()

檔案鎖定:

9.PHP檔案處理

檔案上傳:

9.PHP檔案處理
<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>.';
        }
    }
}
?>