PHP建立檔案(夾)以及目錄操作

2021-06-13 08:19:52 字數 4052 閱讀 6921

一、目錄操作

首先是從目錄讀取的函式,opendir(),readdir(),closedir(),使用的時候是先開啟檔案控制代碼,而後迭代列出:

<?php

$base_dir="filelist/";

$fso=opendir($base_dir);

echo

$base_dir."";

while($flist=readdir($fso))

closedir($fso)

?>

這是講返回檔案目錄下面的檔案已經目錄的程式(0檔案將返回false).

有時候需要知道目錄的資訊,可以使用dirname($path)和basename($path),分別返回路徑的目錄部分和檔名名稱部分,可用disk_free_space($path)返回檢視空間剩餘空間.

建立命令:

mkdir($path,0777):0777是許可權碼,在非window下可用umask()函式設定.

rmdir($path):將刪除路徑在$path的檔案.

二、檔案操作

新建檔案

首先,確定你所要新建檔案所在的目錄

許可權; 建議裝置為777。然後,新建檔案的名稱建議使用絕對路徑。

<?php

$filename="test.txt";

$fp=fopen("$filename", "w+"); //開啟檔案指標,建立檔案

if ( !is_writable($filename) )

//fwrite($filename, "anything you want to write to $filename.";

fclose($fp); //關閉指標

讀檔案

首先是乙個檔案看能不能讀取(許可權問題),或者存在不,我們可以用is_readable函式獲取資訊.:

<?php

$file

= 'dirlist.php';

if (is_readable($file)

== false)

else

?>

判斷檔案存在的函式還有file_exists(下面演示),但是這個顯然無is_readable全面.,當乙個檔案存在的話可以用

<?php

$file

= "filelist.php";

if (file_exists($file)

== false)

$data

= file_get_contents($file);

echo

htmlentities($data);

?>

但是file_get_contents函式在較低版本上不支援,可以先建立檔案的乙個控制代碼,然後用指標讀取全部:

還有一種方式,可以讀取二進位制的檔案:

$data

= implode('',

file($file));

寫檔案

和讀取檔案的方式一樣,先看看是不是能寫:

<?php

$file

= 'dirlist.php';

if (is_writable($file)

== false)

?>

能寫了的話可以使用file_put_contents函式寫入:

<?php

$file

= 'dirlist.php';

if (is_writable($file)

== false)

$data

= '我是可鄙,我想要';

file_put_contents

($file,

$data);

?>

file_put_contents函式在php5中新引進的函式(不知道存在的話用function_exists函式先判斷一下)低版本的php無法使用,可以使用如下方式:

$f = fopen($file,

'w');

fwrite($f,

$data);

fclose($f);

替換之.

寫檔案的時候有時候需要鎖定,然後寫:

function

cache_page($pageurl,$pagedata)

if(!flock($fso,lock_ex))

if(!fwrite($fso,$pagedata))

flock($fso,lock_un);//釋放鎖定

fclose($fso);

return

true; }

複製,刪除檔案

php刪除檔案非常easy,用unlink函式簡單操作:

<?php

$file

= 'dirlist.php';

$result

= @unlink

($file);

if ($result

== false)

else

?>

即可.複製檔案也很容易:

<?php

$file

= 'yang.txt';

$newfile

= 'ji.txt';

# 這個檔案父資料夾必須能寫

if (file_exists($file)

== false)

$result

= copy($file,

$newfile);

if ($result

== false)

?>

可以使用rename()函式重新命名乙個資料夾.其他操作都是這幾個函式組合一下就能實現的.

獲取檔案屬性

我說幾個常見的函式:

<?php

$file

= 'test.txt';

echo

date('r',

filemtime($file));

?>

返回的說unix的時間戳,這在快取技術常用.

相關的還有獲取上次被訪問的時間fileatime(),filectime()當檔案的許可權,所有者,所有組或其它

inode

中的元資料被更新時間,fileowner()函式返回檔案所有者

$owner

= posix_getpwuid(fileowner($file));

(非window系統),ileperms()獲取檔案的許可權,

<?php

$file

= 'dirlist.php';

$perms

= substr(sprintf('%o',

fileperms($file)),

-4);

echo

$perms;

?>

filesize()返回檔案大小的位元組數:

<?php

// 輸出類似:somefile.txt:

1024

bytes

$filename

= 'somefile.txt';

echo

$filename

. ':

' .

filesize($filename)

. '

bytes';

?>

獲取檔案的全部資訊有個返回陣列的函式stat()函式:

<?php

$file

= 'dirlist.php';

$perms

= stat($file);

var_dump($perms);

?>

PHP建立檔案(夾)以及目錄操作

一 目錄操作 首先是從目錄讀取的函式,opendir readdir closedir 使用的時候是先開啟檔案控制代碼,而後迭代列出 base dir filelist fso opendir base dir echo base dir.while flist readdir fso closed...

php 建立資料夾

function mkdirs dir if mkdir dir,0777 chmod dir,777 給目錄操作許可權 return true 開始時我認為,只要給乙個路徑,mkdir就可以建立資料夾,但是事實並不是那樣,單個的mkdir只能建立一級目錄,對於多級的就不行了。1.使用php5新增方...

C 建立資料夾以及檔案

環境 visual studio 2019,windows。如下 create new directory and file if they are not exist region public static void createdirectoryorfile string newdirecto...