PHP 檔案操作

2021-06-29 03:11:58 字數 2979 閱讀 2498

建立檔案

/* 方法一 */

$file = fopen("test.txt", "w");

/* 方法二 */

touch($file);

建立資料夾

mkdir("/web/www/testing/test_dir", 0700);
建立多層資料夾

/* 原生 */

mkdir("/web/www/testing/test_dir", 0700, true);

/* 遞迴 */

function

mkdirs

($path)

建立檔案並寫入內容

/* 方法一 */

function

write_file

($path, $data, $mode = fopen_write_create_destructive)

flock($fp, lock_ex);

fwrite($fp, $data);

flock($fp, lock_un);

fclose($fp);

return

true;

}/* 方法二 */

file_put_contents($file, $data);

刪除檔案

unlink('test.txt');
刪除資料夾

rmdir('test_dir')
刪除資料夾及內容

/* 遞迴 */

function

delete_dir

($path, $del_dir = false, $level = 0)

while (false !== ($filename = @readdir($current_dir)))

} else }}

@closedir($current_dir);

if ($del_dir == true

and$level > 0)

return

true;

}/* 呼叫系統命令 */

function

delete_dir

($path) else

system($str);

}

修改檔案及資料夾名稱

rename($old_name, $new_name);
移動檔案及資料夾

rename($old_path, $new_path);
修改檔案及資料夾許可權

chmod($file, $mode);
修改檔案及資料夾所有者

chown($file, $user);
修改檔案及資料夾的修改和訪問時間

touch($file, $time, $atime);
查詢檔案夾目錄

function

get_filenames

($source_dir, $include_path = false, $_recursion = false)

while (false !== ($file = readdir($fp))) elseif (strncmp($file, '.', 1) !== 0)

}return

$_filedata;

} else

}

獲取檔案內容

function

read_file

($file)

if (function_exists('file_get_contents'))

if (!$fp = @fopen($file, fopen_read))

flock($fp, lock_sh);

$data = '';

if (filesize($file) > 0)

flock($fp, lock_un);

fclose($fp);

return

$data;

}

查詢檔案及資料夾資訊

/* 獲取檔案上次訪問時間 */

fileatime($file);

/* 獲取檔案大小 */

filesize($file);

/* 獲取檔案所有者 */

fileowner($file);

/* 獲取檔案許可權 */

fileperms($file);

/* 獲取檔案上次修改時間 */

filemtime($file);

判斷是否存在

file_exists($file);
判斷許可權

/* 是否可讀 */

is_readable($file);

/* 是否可寫 */

is_writable($file);

/* 是否可執行 */

is_executable($file);

php操作php檔案

聽起來有些暈吧?使用php來操作php頁面。有乙個小的用途,在乙個系統當中可能會有個別的小項不便存入資料庫,但是我們又要經常在其他頁面當中呼叫,而且還需要更新,那麼我們就可以用這種方式來解決。其中遇到幾個小問題,搞了俺半天時間才搞定 比如說 使用者需要更改某乙個標題,但是單獨為這個標題在建立乙個表,...

php檔案操作

1 開啟檔案 resource fopen string filename,string mode bool use include path resource zcontext handle fopen filename,mode 開啟檔案,返回代表此檔案的資源的控制代碼 檔名稱可以使用相對路徑或...

PHP檔案操作

模式引數 r唯讀 w只寫 從頭寫,並刪除原來的內容 x謹慎寫a追加 b二進位制 fp fopen 路徑 模式 flock fp,lock ex 鎖定檔案 fwrite fp,outputstring,strlen outputstring flock fp,lock un 解除鎖定檔案 fclose...