Essential PHP File and Directory Functions Guide
php file functionsphp file operationsphp file handlingphp directory managementphp file upload
Published·Modified·
1. Check if File or Directory Exists
is_file() Function
is_file()— Checks if the given filename is a regular file. Returns TRUE if the file exists and is a regular file; otherwise, returns FALSE.- Official documentation: http://php.net/manual/zh/function.is-file.php
is_dir() Function
is_dir()— Checks if the specified filename is a directory. Returns TRUE if the file exists and is a directory; otherwise, returns FALSE.- Official documentation: http://php.net/manual/zh/function.is-dir.php
file_exists() Function
file_exists()— Checks whether a file or directory exists. Returns TRUE if the specified file or directory exists; otherwise, returns FALSE. This function combines the functionality of the two functions above.- More information: PHP file_exists() Function
2. Reading Files
file_get_contents() Function
file_get_contents()— Reads the entire file into a string.- PHP official usage: http://php.net/manual/zh/function.file-get-contents.php
fread() Function
Example of reading a file:
<?php
$file = fopen("test.txt","r");
fread($file,filesize("test.txt"));
fclose($file);
?>
More usage instructions: PHP fread() Function
3. Writing Files
fwrite() Function
Note: The original text incorrectly labeled this section as fread() again. This section covers writing files using fwrite().
Example:
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Bill Gates\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
More information: PHP File Creation/Write
file_put_contents() Function
file_put_contents()— Writes a string to a file. It performs the same function as callingfopen(),fwrite(), andfclose()sequentially.- Usage:
file_put_contents('filename','filedata') - Official documentation: http://php.net/manual/zh/function.file-put-contents.php
4. Create, Delete, and Copy Directories
mkdir() Function (Create Directory)
- Usage:
mkdir('filename','mode'), defaultmodeis0777(maximum permissions). - Return value: Returns TRUE on success or FALSE on failure.
- Official documentation: http://php.net/manual/zh/function.mkdir.php
rmdir() Function (Delete Directory)
- Return value: Returns TRUE on success or FALSE on failure.
- Official documentation: http://php.net/manual/zh/function.rmdir.php
unlink() Function (Delete File)
- Return value: Returns TRUE on success or FALSE on failure.
- Official documentation: http://php.net/manual/zh/function.unlink.php
copy() Function (Copy File)
- Syntax:
copy(source,destination) - Return value: Copies the file from
sourcetodestination. Returns TRUE on success or FALSE on failure. - More information: PHP copy() Function
rename() Function
rename()— Renames a file or directory.- Syntax:
rename(oldname,newname,context) - Return value: Returns TRUE on success or FALSE on failure.
move_uploaded_file() Function
move_uploaded_file()— Moves an uploaded file to a new location.- Syntax:
move_uploaded_file(file,newloc) - Return value: Returns TRUE on success or FALSE on failure.