<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # PHP 文件系統函數 > 原文: [https://zetcode.com/articles/phpfilesystemfunctions/](https://zetcode.com/articles/phpfilesystemfunctions/) 在本文中,我們介紹了 PHP 中的文件系統函數。 我們處理文件和目錄,確定文件許可權和可用磁盤空間,以及讀寫文件。 對于我們的示例,我們使用 PHP CLI。 [PHP 教程](/lang/php)是有關 ZetCode 的 PHP 語言的簡要教程。 PHP 具有用于處理文件和目錄的豐富函數集。 PHP 包含低級和高級文件系統函數。 `fopen()`函數是一個低級函數的示例; 它是類似 C 函數的薄包裝。 `file()`函數是高級 PHP 文件系統函數的示例。 ## 文件大小和類型 `filesize()`函數返回給定文件的大小。 大小以字節為單位指定。 `get_filesize.php` ```php <?php $filename = "fruits.txt"; $fsize = filesize($filename); echo "The file size is: $fsize bytes\n"; ?> ``` 在示例中,我們確定`fruits.txt`文件的大小。 該文件位于當前工作目錄中,即與 PHP 腳本位于同一目錄中。 ```php $ php get_filesize.php The file size is: 40 bytes ``` `fruits.txt`文件的大小為 40 個字節。 `filetype()`函數獲取文件的類型。 可能的返回值是:`fifo`,`char`,`dir`,`block`,`link`,`file`,`socket`和`unknown`。 `file_types.php` ```php <?php echo filetype("myfile") . PHP_EOL; echo filetype(".") . PHP_EOL; echo filetype("/dev/tty1") . PHP_EOL; echo filetype("/var/run/cups/cups.sock") . PHP_EOL; ?> ``` 該腳本確定四個文件的類型。 ```php $ php file_types.php file dir char socket ``` 這四個文件是常規文件,目錄,字符設備和套接字。 ## 文件存在 我們可能想使用一個不存在的文件,這可能發生。 `file_exists()`函數可用于防止這種情況。 `file_existence.php` ```php <?php if ($argc != 2) { exit("Usage: file_existence.php filename\n"); } $filename = $argv[1]; $r = file_exists($filename); if (!$r) { exit("Cannot determine the size of the file; the file does not exist\n"); } $fsize = filesize($filename); echo "The file size is: $fsize bytes\n"; ?> ``` 該腳本在計算給定文件的大小之前會檢查其是否存在。 ```php if ($argc != 2) { exit("Usage: file_existence.php filename\n"); } ``` `$argc`是一個特殊變量,其中包含傳遞給腳本的參數數量。 我們需要兩個參數:一個腳本名稱和另一個作為參數傳遞的文件名。 ```php $filename = $argv[1]; ``` `$argv`是傳遞給腳本的參數數組。 我們得到第二個要素。 ```php $r = file_exists($filename); if (!$r) { exit("Cannot determine the size of the file; the file does not exist\n"); } ``` 我們使用`file_exists()`函數檢查文件是否存在。 如果它不存在,我們將以一條消息終止腳本。 ```php $ php file_existence.php fruits.txt The file size is: 40 bytes ``` 這是`file_existence.php`腳本的示例輸出。 在下面的示例中,我們創建一個新文件,將其刪除,然后檢查其是否存在。 `touch()`函數設置文件的訪問和修改時間。 如果該文件不存在,則會創建它。 `unlink()`函數刪除文件。 `file_existence2.php` ```php <?php $filename = "newfile.txt"; if (file_exists($filename)) { echo "The file $filename exists\n"; } else { echo "The file $filename does not exist\n"; $r = touch($filename); if (!$r) { exit("Failed to touch $filename file\n"); } else { echo "The file $filename has been created\n"; } } $r = unlink($filename); if ($r) { echo "The file $filename was deleted\n"; } else { exit("Failed to delete $filename file\n"); } if (file_exists($filename)) { echo "The file $filename exists\n"; } else { echo "The file $filename does not exist\n"; } ?> ``` 在代碼示例中,我們利用了所有三個函數:`file_exists()`,`touch()`和`unlink()`。 ```php $r = touch($filename); ``` `touch()`函數用于創建一個名為`newfile.txt`的新文件。 ```php if (!$r) { exit("Failed to touch $filename file\n"); } else { echo "The file $filename has been created\n"; } ``` 如果`touch()`函數失敗,則會打印一條錯誤消息。 許多 PHP 函數在失敗時會返回`false`值。 ```php $r = unlink($filename); ``` `unlink()`函數刪除文件。 ```php $ php file_existence2.php The file newfile.txt does not exist The file newfile.txt has been created The file newfile.txt was deleted The file newfile.txt does not exist ``` 這是`file_existence2.php`的輸出。 ## 復制和重命名文件 `copy()`函數復制文件,`rename()`重命名文件。 如果目標文件已經存在,它將被覆蓋。 `copy_file.php` ```php <?php $r = copy("myfile.txt", "myfile2.txt"); if ($r) { echo "Successfully copied file\n"; } else { exit("Failed to copy file\n"); } ?> ``` 該腳本將復制文件。 `rename_file.php` ```php <?php $r = rename("myfile2.txt", "myfile_back.txt"); if ($r) { echo "Successfully renamed file\n"; } else { exit("Failed to rename file\n"); } ?> ``` 在此腳本中,我們使用`rename()`函數將`myfile2`文件重命名為`myfile_back`。 ## `E_WARNING` 一些文件系統函數在失敗時發出`E_WARNING`。 這是運行時警告(非致命錯誤)。 腳本的執行不會停止。 PHP 在這方面不一致。 并非所有文件系統函數都發出此警告-大多數函數僅在失敗時才返回錯誤值。 `custom_error_handler.php` ```php <?php set_error_handler("mywarning_handler", E_WARNING); $r = unlink('image1.png'); if ($r) { echo "File successfully deleted\n"; } function mywarning_handler($errno, $errstr) { echo "Failed to delete file\n"; echo "$errstr: $errno\n"; } ?> ``` 在腳本中,我們刪除文件并提供自定義錯誤處理器。 ```php set_error_handler("mywarning_handler", E_WARNING); ``` 使用`set_error_handler()`函數設置自定義錯誤處理器。 ```php function mywarning_handler($errno, $errstr) { echo "Failed to delete file\n"; echo "$errstr: $errno\n"; } ``` 處理器將收到錯誤號和錯誤字符串作為參數。 ```php $ php custom_error_handler.php Failed to delete file unlink(image1.png): No such file or directory: 2 ``` 當沒有`image1.png`要刪除時,`custom_error_handler.php`提供此輸出。 ## 目錄 `dirname()`函數返回父目錄的路徑。 從 PHP 7 開始,我們可以提供一個可選的`levels`參數,該參數指示要上升的父目錄的數量。 `parent_directories.php` ```php <?php $home_dir = getenv("HOME"); echo dirname($home_dir). PHP_EOL; echo dirname("/etc/") . PHP_EOL; echo dirname(".") . PHP_EOL; echo dirname("/usr/local/lib", 2) . PHP_EOL; ?> ``` 在腳本中,我們打印四個目錄的父目錄。 ```php $home_dir = getenv("HOME"); ``` 我們使用`getenv()`函數來獲取當前用戶的主目錄。 ```php echo dirname($home_dir). PHP_EOL; ``` 此行顯示用戶主目錄的父目錄。 ```php echo dirname(".") . PHP_EOL; ``` 在這里,我們打印當前工作目錄的父目錄。 ```php echo dirname("/usr/local/lib", 2) . PHP_EOL; ``` 在這一行中,我們打印`/usr/local/lib`目錄的第二個父目錄。 ```php $ php parent_directories.php /home / . /usr ``` 這是`parent_directories.php`的輸出。 `getcwd()`函數返回當前工作目錄,`chdir()`函數將當前工作目錄更改為新目錄。 `current_directory.php` ```php <?php $cd = getcwd(); echo "Current directory:" . $cd . PHP_EOL; chdir(".."); $cd2 = getcwd(); echo "Current directory:" . $cd2 . PHP_EOL; ?> ``` 該腳本可與`getcmd()`和`chdir()`函數一起使用。 ```php $ php current_directory.php Current directory:/home/janbodnar/prog/phpfiles Current directory:/home/janbodnar/prog ``` 這是腳本的示例輸出。 ## 列出目錄 在以下五個示例中,我們列出了目錄的內容。 有幾種方法可以完成此任務。 `list_dir1.php` ```php <?php $folder = '/home/janbodnar/prog'; $fh = opendir($folder); if ($fh === false) { exit("Cannot open directory\n"); } while (false !== ($entry = readdir($fh))) { echo "$entry\n"; } closedir($fh); ?> ``` `opendir()`函數打開目錄句柄。 `readdir()`函數從目錄句柄讀取條目。 在腳本末尾使用`closedir()`函數關閉目錄的句柄。 `is_dir()`函數判斷文件名是否為目錄,`is_file()`函數判斷文件名是否為文件。 `list_dir2.php` ```php <?php $folder = '/home/janbodnar/prog/'; $fh = opendir($folder); if ($fh === false) { exit("Cannot open directory\n"); } $dirs = []; $files = []; while (false !== ($entry = readdir($fh))) { if (is_dir($folder . '/' . $entry)) { array_push($dirs, $entry); } if (is_file($folder . '/' . $entry)) { array_push($files, $entry); } } echo "Directories:\n"; foreach ($dirs as $dr) { echo "$dr\n"; } echo "Files:\n"; foreach ($files as $myfile) { echo "$myfile\n"; } closedir($fh); ?> ``` 在第二個示例中,我們將條目分為子目錄和文件。 該腳本首先打印子目錄,然后打印所檢查目錄的文件。 ```php if (is_dir($folder . '/' . $entry)) { array_push($dirs, $entry); } ``` 必須提供`is_dir()`函數的目錄的完整路徑。 `glob()`函數查找與模式匹配的路徑名。 `list_dir3.php` ```php <?php foreach (glob('/home/janbodnar/*', GLOB_ONLYDIR) as $dir) { echo "$dir\n"; } ?> ``` 使用`GLOB_ONLYDIR`標志,`glob()`函數僅返回與模式匹配的目錄條目。 `scandir()`是高級函數,用于列出指定路徑內的文件和目錄。 該函數從目錄返回文件和目錄的數組。 `list_dir4.php` ```php <?php $files = scandir('.', SCANDIR_SORT_DESCENDING); print_r($files); ?> ``` 該腳本打印當前工作目錄的文件和子目錄的數組。 `SCANDIR_SORT_DESCENDING`標志以字母降序對條目進行排序。 在前面的示例中,我們僅列出了一個目錄的內容; 我們沒有包括子目錄的元素。 使用`RecursiveDirectoryIterator`和`RecursiveIteratorIterator`類,我們可以輕松地使用遞歸遍歷文件系統目錄。 換句話說,我們遍歷所有子目錄,直到列出目錄樹中的所有項目。 `list_dir5.php` ```php <?php $folder = '/home/janbodnar/prog/'; $rdi = new RecursiveDirectoryIterator($folder); $rii = new RecursiveIteratorIterator($rdi); foreach ($rii as $filename) { echo "$filename\n"; } ?> ``` 該腳本將打印所有深度級別的給定目錄的所有項目。 ## 路徑 路徑是計算機文件的完整指定名稱,包括文件在文件系統目錄結構中的位置。 `realpath()`函數返回規范的絕對路徑名,`basename()`函數返回路徑的尾隨名稱部分。 `paths.php` ```php <?php echo realpath("myfile.txt") . PHP_EOL; echo basename("/home/janbodnar/prog/phpfiles/myfile.txt") . PHP_EOL; echo basename("/home/janbodnar/prog/phpfiles/myfile.txt", ".txt") . PHP_EOL; ?> ``` 該腳本使用`realpath()`和`basename()`函數。 ```php echo basename("/home/janbodnar/prog/phpfiles/myfile.txt", ".txt") . PHP_EOL; ``` 如果我們指定第二個參數,即后綴名,則它也會從路徑名中刪除。 ```php $ php paths.php /home/janbodnar/prog/phpfiles/myfile.txt myfile.txt myfile ``` 這是`paths.php`示例的輸出。 `pathinfo()`函數返回有關文件路徑的信息。 `path_info.php` ```php <?php $path_parts = pathinfo('myfile.txt'); echo $path_parts['dirname'] . PHP_EOL; echo $path_parts['basename'] . PHP_EOL; echo $path_parts['extension'] . PHP_EOL; echo $path_parts['filename'] . PHP_EOL; ?> ``` 該函數返回一個包含以下元素的關聯數組:目錄名稱,基礎名稱,擴展名(如果有)和文件名。 ```php $ php path_info.php . myfile.txt txt myfile ``` 這是輸出。 ## 創建文件 `fopen()`函數打開文件或 URL。 函數的第一個參數是文件名,第二個是打開窗口的模式。 例如,`'r'`模式打開僅用于讀取,`'w'`模式僅用于寫入。 如果我們以`'w'`模式打開文件,但該文件不存在,則會創建該文件。 可以在 [PHP 手冊中的`fopen()`](http://php.net/manual/en/function.fopen.php)中找到模式列表。 `fopen()`返回文件的句柄。 這是一個用于操作文件的對象。 例如,我們將其傳遞給`fwrite()`函數以寫入文件。 `create_file.php` ```php <?php $filename = "names.txt"; if (file_exists($filename)) { exit("The file already exists\n"); } $fh = fopen($filename, 'w'); if ($fh === false) { exit("Cannot create file\n"); } echo "Successfully created file\n"; fclose($fh); ?> ``` 該示例創建一個名為`names.txt`的新文件。 ```php if (file_exists($filename)) { exit("The file already exists\n"); } ``` 首先,我們檢查文件是否存在。 ```php $fh = fopen('names.txt', 'w'); ``` 創建`names.txt`文件,并返回該文件的句柄。 ```php fclose($fh); ``` 我們使用`fclose()`函數關閉文件句柄。 ## 讀取文件 在下一個示例中,我們將讀取文件內容。 `fread()`從句柄引用的文件指針中讀取最多`length`個字節。 讀取`length`字節或到達 EOF(文件末尾)后,讀取停止。 `read_file.php` ```php <?php $fh = fopen('balzac.txt', 'r'); if ($fh === false) { exit("Cannot open file for reading\n"); } while (!feof($fh)) { $chunk = fread($fh, 1024); if ($chunk === false) { exit("Cannot read from file\n"); } echo $chunk; } fclose($fh); ?> ``` 該示例使用`fread()`函數讀取整個文件,并將其輸出到控制臺。 ```php while (!feof($fh)) { $chunk = fread($fh, 1024); if ($chunk === false) { exit("Cannot read from file\n"); } echo $chunk; } ``` `feof()`測試文件指針上的文件結尾。 `fread()`每 1 KB 的塊讀取文件,直到達到 EOF。 ```php $ php read_file.php balzac.txt Honoré de Balzac, (born Honoré Balzac, 20 May 1799 – 18 August 1850) was a French novelist and playwright. His magnum opus was a sequence of short stories and novels collectively entitled La Comédie Humaine, which presents a panorama of French life in the years after the 1815 Fall of Napoleon Bonaparte. ``` 這是`read_file.php`示例的輸出。 在第二個示例中,我們利用`fgets()`函數從文件句柄讀取一行。 `read_file2.php` ```php <?php $fh = fopen('balzac.txt', 'r'); if ($fh === false) { exit("Cannot open file for reading\n"); } while (!feof($fh)) { $line = fgets($fh); if ($line === false) { exit("Cannot read from file\n"); } echo $line; } fclose($fh); ?> ``` 該示例逐行讀取`balzac.txt`文件的內容。 `file()`是將整個文件讀入數組的高級函數。 `read_file3.php` ```php <?php $lines = file('balzac.txt'); if ($lines === false) { exit("Cannot read file\n"); } foreach ($lines as $line) { echo $line; } ?> ``` 在此示例中,我們使用`file()`函數一次讀取了整個文件。 我們使用`foreach`循環遍歷返回的數組。 `file_get_contents()`是另一個高級函數,它將整個文件讀取為字符串。 `read_file4.php` ```php <?php $content = file_get_contents('balzac.txt'); if ($content === false) { exit("Cannot read file\n"); } echo "$content"; ?> ``` 該示例使用`file_get_contents()`函數一次讀取了整個文件。 它以字符串形式返回數據。 ## 讀取格式化的數據 `fscanf()`函數根據格式分析文件的輸入。 每次對`fscanf()`的調用都會從文件中讀取一行。 ```php $ cat items.txt coins 5 pens 6 chairs 12 books 20 ``` 我們將解析`items.txt`文件。 `read_formatted_data.php` ```php <?php $fh = fopen("items.txt", "r"); if ($fh === false) { exit("Cannot read file\n"); } while ($data = fscanf($fh, "%s %d")) { list($item, $quantity) = $data; echo "$item: $quantity\n"; } fclose($fh); ?> ``` `fscanf()`函數使用格式說明符來讀取字符串和數字。 ## 讀取網頁 PHP 文件系統函數還可用于讀取網頁。 `read_page.php` ```php <?php $ph = fopen("http://www.something.com", "r"); if ($ph === false) { exit("Failed to open stream to URL\n"); } while (!feof($ph)) { $buf = fread($ph, 1024); if ($buf === false) { exit("Cannot read page\n"); } echo $buf; } fclose($ph); ?> ``` 我們從一個名為`www.something.com`的小型網站上閱讀了一個頁面。 ```php $ph = fopen("http://www.something.com", "r"); ``` 使用`fopen()`函數,我們打開網頁的句柄。 ```php while (!feof($ph)) { $buf = fread($ph, 1024); if ($buf === false) { exit("Cannot read page\n"); } echo $buf; } ``` 我們閱讀網頁直至結束。 `feof()`用于測試網頁的結尾。 使用`fread()`函數以 1KB 的塊讀取頁面。 ```php $ php read_page.php <html><head><title>Something.</title></head> <body>Something.</body> </html> ``` 這是`read_page.php`腳本的輸出。 以下示例使用高級函數來讀取同一網頁。 `read_page2.php` ```php <?php $page = file_get_contents('http://www.something.com'); if ($page === false) { exit("Cannot read page\n"); } echo $page; ?> ``` `file_get_contents()`一次讀取整個網頁。 `fgetss()`函數從文件句柄讀取一行并剝離 HTML 標簽。 `read_page3.php` ```php <?php $ph = fopen("http://www.something.com", "r"); if ($ph === false) { exit("Failed to open stream to URL\n"); } while (!feof($ph)) { $buf = fgetss($ph, 1024); if ($buf === false) { exit("Cannot read from page\n"); } echo $buf; } fclose($ph); ?> ``` 我們閱讀`www.something.com`網頁的內容并剝離其 HTML 標簽。 ```php $ php read_page3.php Something. Something. ``` 這是示例的輸出; 輸出包含頁面正文中的標題和文本。 ## 寫入文件 `fwrite()`函數將字符串寫入文件句柄引用的文件。 `write_file.php` ```php <?php $fh = fopen('names.txt', 'w'); if ($fh === false) { exit("Cannot open file\n"); } $r = fwrite($fh, 'Jane' . PHP_EOL); check_retval($r); $r = fwrite($fh, 'Lucy' . PHP_EOL); check_retval($r); $r = fwrite($fh, 'Mark' . PHP_EOL); check_retval($r); $r = fwrite($fh, 'Lubos' . PHP_EOL); check_retval($r); fclose($fh); function check_retval($val) { if ($val === false) { exit("Cannot write to file\n"); } } ?> ``` 我們在寫入模式下打開`names.txt`并向其中寫入四行。 ```php $fh = fopen('names.txt', 'w'); ``` `fopen()`函數以寫入模式打開文件。 如果文件不存在,則會自動創建。 ```php $r = fwrite($fh, 'Jane' . PHP_EOL); ``` 使用`fwrite()`函數,我們在文件中寫入一行。 該函數將文件句柄作為其第一個參數。 ```php $ php write_file.php $ cat names.txt Jane Lucy Mark Lubos ``` 我們寫入`names.txt`文件并檢查其內容。 我們可以使用高級`file_put_contents()`方法將字符串一次性寫入文件。 `write_file2.php` ```php <?php $filename = "names.txt"; $buf = file_get_contents($filename); if ($buf === false) { exit("Cannot get file contents\n"); } $buf .= "John\nPaul\nRobert\n"; $r = file_put_contents($filename, $buf); if ($r === false) { exit("Cannot write to file\n"); } ?> ``` 在示例中,我們使用`file_get_contents()`函數讀取`names.txt`文件的內容,并使用`file_put_contents()`函數附加新字符串。 ## 可讀,可寫,可執行文件 `is_readable()`,`is_writable()`和`is_executable()`函數檢查文件是否可讀,可寫和可執行。 `rwe.php` ```php <?php $filename = "myfile.txt"; echo get_current_user() . PHP_EOL; if (is_readable($filename)) { echo "The file can be read\n"; } else { echo "Cannot read file\n"; } if (is_writable($filename)) { echo "The file can be written to\n"; } else { echo "Cannot write to file\n"; } if (is_executable($filename)) { echo "The file can be executed\n"; } else { echo "Cannot execute file\n"; } ?> ``` 我們在`myfile.txt`文件上運行三個函數。 該腳本檢查當前用戶的這些屬性。 ```php $ php rwe.php janbodnar The file can be read The file can be written to Cannot execute file ``` `janbodnar`用戶可以讀寫該文件,但不能執行。 ## 文件時間 Linux 上有三種文件時間:上次訪問時間,上次更改時間和上次修改時間。 以下 PHP 函數確定這些時間:`fileatime()`,`filectime()`和`filemtime()`。 `file_times..php` ```php <?php $filename = "myfile.txt"; $atime = fileatime($filename); $ctime = filectime($filename); $mtime = filemtime($filename); echo date("F d, Y H:i:s\n", $atime); echo date("F d, Y H:i:s\n", $ctime); echo date("F d, Y H:i:s\n", $mtime); ?> ``` 該腳本打印`myfile.txt`文件的文件時間。 ```php $ php file_times.php April 20, 2016 17:52:54 April 20, 2016 17:53:33 April 20, 2016 17:52:29 ``` 這是`file_times.php`腳本的示例輸出。 ## 文件權限 文件系統對不同的用戶和用戶組強制執行文件權限。 `fileperms()`函數獲得文件許可; 它以數字方式返回文件的權限。 `file_permissions.php` ```php <?php $perms = fileperms("myfile.txt"); echo decoct($perms & 0777) . PHP_EOL; $info .= (($perms & 0x0100) ? 'r' : '-'); $info .= (($perms & 0x0080) ? 'w' : '-'); $info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-')); $info .= (($perms & 0x0020) ? 'r' : '-'); $info .= (($perms & 0x0010) ? 'w' : '-'); $info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-')); $info .= (($perms & 0x0004) ? 'r' : '-'); $info .= (($perms & 0x0002) ? 'w' : '-'); $info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-')); echo "$info\n"; ?> ``` 該腳本確定`myfile.txt`的文件許可權。 權限以 Unix 樣式打印到控制臺。 ```php echo decoct($perms & 0777) . PHP_EOL; ``` Unix 上的權限傳統上以八進制表示形式編寫。 `decoct()`函數將十進制表示形式轉換為八進制形式。 ```php $info .= (($perms & 0x0100) ? 'r' : '-'); ``` 在這一行中,我們檢查文件權限是否允許文件所有者讀取它。 ```php $ php file_permissions.php 660 rw-rw---- ``` 這是`file_permissions.php`腳本的示例輸出。 可以使用`chmod()`函數更改文件權限。 `.php` ```php <?php $perms1 = fileperms("myfile.txt"); echo decoct($perms1 & 0777) . PHP_EOL; $r = chmod("myfile", 0660); if ($r) { echo "File mode successfully changed\n"; } else { exit("Failed to change file mode\n"); } $perms2 = fileperms("myfile"); echo decoct($perms2 & 0777) . PHP_EOL; ?> ``` 該腳本更改`myfile.txt`文件的權限。 ```php $r = chmod("myfile", 0660); ``` `chmod`函數在其第二個參數中接受權限作為八進制值。 八進制值以 0 開頭。 ```php $ php file_permissions2.php 664 File mode successfully changed 660 ``` 文件的權限從 664 更改為 660。 ## CSV 文件格式 `fgetcsv()`函數從 CSV(逗號分隔值)文件中讀取一行; 它返回一個包含讀取字段的索引數組。 `fputcsv()`函數將一行格式設置為 CSV 并將其寫入文件。 `csv_output.php` ```php <?php $nums = [1, 2, 5, 3, 2, 6, 4, 2, 4, 8, 7, 3, 8, 5, 4, 3]; $fh = fopen('numbers.csv', 'w'); if ($fh === false) { exit("Failed to open file\n"); } $r = fputcsv($fh, $nums); if ($r === false) { exit("Failed to write values\n"); } echo "The values have been successfully written\n"; fclose($fh); ?> ``` 該腳本將數組中的數字寫入 CSV 文件。 ```php $ php csv_output.php The values have been successfully written $ cat numbers.csv 1,2,5,3,2,6,4,2,4,8,7,3,8,5,4,3 ``` 我們運行腳本并檢查文件內容。 在以下示例中,我們從 CSV 文件讀取數據。 `csv_input.php` ```php <?php $fh = fopen('numbers.csv', 'r'); if ($fh === false) { exit("Failed to open file\n"); } while (($data = fgetcsv($fh)) !== false) { $num = count($data); for ($i=0; $i < $num; $i++) { echo "$data[$i] "; } } echo "\n"; fclose($fh); ?> ``` 該腳本使用`fgetcsv()`從`numbers.csv`文件中讀取值,并將其打印到控制臺中。 ```php $ php csv_input.php 1 2 5 3 2 6 4 2 4 8 7 3 8 5 4 3 ``` 這是`csv_input.php`腳本的輸出。 ## 磁盤空間 `disk_total_space()`函數以字節為單位返回文件系統或磁盤分區的總大小,`disk_total_space()`函數以字節為單位返回文件系統或磁盤分區上的可用空間。 `disk_space.php` ```php <?php const BYTES_IN_GIGABYTE = 1073741824; $total_space_bytes = disk_total_space("/"); if ($total_space_bytes === false) { exit("The disk_total_space() failed\n"); } $free_space_bytes = disk_free_space("/"); if ($free_space_bytes === false) { exit("The disk_free_space() failed\n"); } $total_space_gb = floor($total_space_bytes / BYTES_IN_GIGABYTE); $free_space_gb = floor($free_space_bytes / BYTES_IN_GIGABYTE); echo "Total space: $total_space_gb GB\n"; echo "Free space: $free_space_gb GB\n"; ?> ``` 該腳本計算根分區上的總空間和可用空間。 該空間被轉換為千兆字節。 ```php $ php disk_space.php Total space: 289 GB Free space: 50 GB ``` This is a sample output of the script. 您可能也對以下相關教程感興趣: [PHP PDO 教程](/php/pdo/), [PHP 教程](/lang/php/)。 ## 數據來源 * [PHP 文件系統函數](http://php.net/manual/en/ref.filesystem.php) 在本文中,我們介紹了 PHP 文件系統函數。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看