PHPファイル関数--ピエロ

11096 ワード

ファイル関数
1、basename(); – パスの「ファイル名」セクションを返します.
string basename ( string $path [, string $suffix ] )
//                     ,           。
    :path     。  Windows  ,  (/)    (\)          。         (/)

   suffix         suffix    ,          。

     :   path        。

$path = 'd:/test/test.txt';

echo basename($path);
echo "
"; echo basename($path,'.txt');

2、dirname(); – 「≪パス|Path|emdw≫」の「≪ディレクトリ|Directory|emdw≫」セクションに戻る
string dirname ( string $path )
//                     ,               。
    :path,    。  Windows  ,  (/)    (\)          。         (/)。

     :   path     。     path      ,      ('.'),      。       path      /component(            )        。

$path = 'd:/test/test.txt';

echo basename($path);
echo "
"; echo basename($path,'.txt'); echo "
"; echo dirname($path);

3、pathinfo(); --ファイルパスの情報を返します
mixed pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )
//pathinfo()             path    。               options。
    : path        。
     options      ,        ;    :PATHINFO_DIRNAME,PATHINFO_BASENAME  PATHINFO_EXTENSION   PATHINFO_FILENAME。       options           。
     :       options ,              array:dirname,basename   extension(   ),   filename。   
$path = 'd:/test/test.txt';
var_dump(pathinfo($path));

4、filetype();–取得ファイルタイプ
string filetype ( string $filename )
//       。
    : filename      。
     :        。       fifo,char,dir,block,link,file   unknown。        FALSE。   stat                filetype()        E_NOTICE   。
$path = 'd:/test/test.txt';
echo filetype($path);
//  file

5、fstat()とstat()
 ⑴、fstat()-                

array fstat ( resource $handle )
//        handle           。     stat()     ,                     。
    : handle       ,      fopen()     resource(  )。

        :                 ,               stat()    。  

  ⑵、stat() --       

array stat ( string $filename )
//    filename           。   filename      ,                ,       。
//lstat()   stat()   ,              。
    :filename      .

$path = 'd:/test/test.txt';

$fp = fopen("d:/test/test.txt","r");
$fstat = fstat($fp);
fclose($fp);
var_dump($fstat);

6、filesize();–取得ファイルサイズ
int filesize ( string $filename )
//         。
    :filename      。

     :          ,       FALSE       E_WARNING     。


//  :d:/test/test.txt: 12 bytes

7、disk_free_space(); – ディレクトリの空き領域を返します
float disk_free_space ( string $directory )
//               ,                          。
    :directory              。

header("Content-Type:Text/html;charset=utf8");
$path = 'd:/test/test.txt';
$df = disk_free_space("d:/");
echo $df."  ";

8、disk_total_space(); --1つのディレクトリに戻るディスクの合計サイズ
float disk_total_space ( string $directory )
//               ,                          。 【   】                     ,                               。   Unix   Windows 200x/XP                    ,              。
    :directory              

9、fopen( f i l e p a t h , filepath, filepath,mode)
resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )
//fopen()   filename               
    :filename    filename   "scheme://..."    ,       URL,PHP         (        )      。             ,PHP                        filename                 。

    

fopen()   mode       
mode	  
'r'	      ,          。
'r+'	      ,          。
'w'	      ,                   。             。
'w+'	      ,                   。             。
'a'	      ,           。             。
'a+'	      ,           。             。
'x'	          ,          。       ,  fopen()         FALSE,      E_WARNING        。             。        open(2)        O_EXCL|O_CREAT       。
'x+'	          ,       'x'   。


10、file();–ファイル全体を配列に読み込む
array file ( string $filename [, int $flags = 0 [, resource $context ]] )
//            。
    :filename      。

     flags      flags             :

         1、FILE_USE_INCLUDE_PATH   include_path      。 2、FILE_IGNORE_NEW_LINES                   3、FILE_SKIP_EMPTY_LINES     。
     context        ,  stream_context_create()  。


 $line) {
    echo "Line #{$line_num} : " . htmlspecialchars($line) . "

"; } // web 。 file_get_contents()。 $html = implode('', file('http://www.example.com/')); // PHP 5 $trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); ?>

11、file_get_contents();-- ファイル全体を文字列に読み込む
string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )
//  file()   ,    file_get_contents()           。     offset               maxlen    。    ,file_get_contents()     FALSE。
    :filename:          。

       use_include_path:As of PHP 5 the FILE_USE_INCLUDE_PATH can be used to trigger include path search.

     context:A valid context resource created with stream_context_create().           context,    NULL    。


header("Content-Type:Text/html;charset=utf8");
// <= PHP 5
$file = file_get_contents('d:/test/test.txt', true);
echo $file.'
'; // > PHP 5 $file = file_get_contents('d:/test/test.txt', FILE_USE_INCLUDE_PATH); echo $file; // //this is test //this is test

12、fgets();–ファイルポインタからローを読み込む
string fgets ( resource $handle [, int $length ] )
//          。
    :handle:          ,      fopen()   fsockopen()        (     fclose()   )。

            length:  handle                    length - 1       。     (       )、EOF         length - 1      (         )。      length,     1K,    1024   。

13、ftell();-- ファイルポインタの読み取り/書き込みの場所を返します
int ftell ( resource $handle )
//    handle           ,           。
    :handle :           ,          fopen()   popen()        。     (    "a"     )  ftell()         。


header("Content-Type:Text/html;charset=utf8");
// opens a file and read some data
$fp = fopen("d:/test/test.txt", "r");
$data = fgets($fp, 4);
// where are we ?
echo ftell($fp); //   3
fclose($fp);

14、fseek();–ファイルポインタでの位置付け
int fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] )
//   handle               。                ,   whence         offset。
     :handle:      ,      fopen()     resource(  )。

      offset:   。            ,    offset       ,    whence   SEEK_END。

      whence values are: 1、SEEK_SET -        offset   。2、SEEK_CUR -             offset。2、SEEK_END -            offset。

header("Content-Type:Text/html;charset=utf8");
$fp = fopen('d:\test\test.txt', 'r');
// read some data
$data = fgets($fp, 4096);
// move back to the beginning of the file
// same as rewind($fp);
 fseek($fp, 0);

15、flock();–軽便なコンサルティングファイルロック
bool flock ( resource $handle , int $operation [, int &$wouldblock ] )
//flock()                       /    (       Unix         Windows)。
    :handle       ,      fopen()     resource(  )。
 operation         :1、LOCK_SH      (     )。2、LOCK_EX       (     。3、LOCK_UN     (       )。

                 flock()       ,   LOCK_NB(Windows      )。

      wouldblock:         (EWOULDBLOCK       ),              TRUE。(Windows     )
if (flock($fp, LOCK_EX)) {  //        
    ftruncate($fp, 0);      // truncate file
    fwrite($fp, "Write something here
"); fflush($fp); // flush output before releasing the lock flock($fp, LOCK_UN); // } else { echo "Couldn't get the lock!"; } fclose($fp);

16、is_readable--指定されたファイル名が読み取り可能かどうかを判断します.
bool is_readable ( string $filename )
//               。
    :filename:     。

     :    filename                   TRUE,     FALSE。 
$filename = 'd:\test\test.txt';
if (is_readable($filename)) {
    echo 'The file is readable';
} else {
    echo 'The file is not readable';
}
//The file is readable

17、is_writeable–指定したファイル名が書き込み可能かどうかを判断します
bool is_writable ( string $filename )
//              TRUE。filename                      。
    :filename         。
$filename = 'd:\test\test.txt';
if (is_writeable($filename)) {
    echo 'The file is writeable';
} else {
    echo 'The file is not writeable';
}
//The file is writeable

18、chown(); – ファイルの所有者を変更
bool chown ( string $filename , mixed $user )
//      filename          user(        ID   )。                 。
    :filename:    。

     user:      。

二、目次関数1、is_dir();–指定したファイル名がディレクトリであるかどうかを判断
bool is_dir ( string $filename )
//              。
    :filename:                TRUE。   filename        ,                。

$filename = 'd:\test\test.txt';
var_dump(is_dir('$filename'));    //bool(false) 
var_dump(is_dir('d:\test'));        //bool(true)

2、mkdir();–新規ディレクトリ
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
//        pathname      。
    :pathname:     。

  mode:    mode   0777,           。   mode          chmod()   。

mkdir("d:/test/test1", 0700);

3、opendir();–ディレクトリハンドルを開く
resource opendir ( string $path [, resource $context ] )
//        ,       closedir(),readdir()   rewinddir()    。
    :path         

     context            Streams API   。

4、readdir();–ディレクトリハンドルからエントリを読み込む
string readdir ([ resource $dir_handle ] )
//              。               。
    :dir_handle       resource,    opendir()   

header("Content-Type:Text/html;charset=utf8");
if ($handle = opendir('d:/test')) {
    echo "Directory handle: $handle
"; echo "Files:
"; /* */ while (false !== ($file = readdir($handle))) { echo "$file
"; } /* while ($file = readdir($handle)) { echo "$file
"; } */ closedir($handle);

}