A set of basic functions that will help work with files through language PHP.
For example, imagine that we work with txt file named file.txt. File content = “123456”
1. copy – copy file to the folder, and can rename it:
copy('file.txt', 'folder/file2.txt');//copies the file to a new directory and gives it a new name "file2.txt". Return true or false //If you want to rename a file, use the rename() function.
2. file_exists – checks for the existence of the specified file or directory
/*if (file_exists(__DIR__ . '/folder/file.txt')) { echo 'OK'; } else { echo 'NO'; }*/ //returns true or false
3. file_get_contents – reads the contents of a file into a string
$file = file_get_contents('file.txt');//return string "123456". The function allows many parameters. More in the documentation - https://www.php.net/manual/function.file-get-contents.php
4. file_put_contents – writes data to a file
The function is identical to successive calls to fopen(), fwrite(), and fclose().
file_put_contents('file2.txt', $file, FILE_APPEND); //if file not exist, will automatically create it //data for write. Can be of type string, array, or stream resource. //FILE_APPEND - will add data to the end of the file
5. file – reads the contents of a file and puts it into an array
file('file2.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); //Returns the file as an array. Each element of the array corresponds to a line in the file, including newlines. On error, file() returns false. //FILE_IGNORE_NEW_LINES - Skip newline at the end of each array element //FILE_SKIP_EMPTY_LINES - Skip blank lines
6. move_uploaded_file – move the downloaded file to a new location
If file.txt not an uploaded file, no action is taken and move_uploaded_file() returns false.
move_uploaded_file($_FILES['file']['tmp_name'], "/files/file2.txt");//1 parameter from where to move, 2 parameter where. You can change the name when moving
7. rename – renames a file or directory
rename(__DIR__ . '/file.txt', __DIR__ . '/folder/file2.txt');//tries to rename from file.txt to file2.txt, move file between directories if necessary //returns true or false
8. mkdir — create a directory
mkdir('test', 0644);//will try to create test directories and set its access rights to 0644 mkdir('1/2/3', 0644, true);//true - allows you to create new directories
9. rmdir — deletes a directory
rmdir('1');//attempts to delete a directory named "1". The directory must be empty and must have the necessary permissions to do so. //return true or false
10. unlink — delete file
unlink(__DIR__ . '/folder/file.txt');//Returns true on success or false on error.
11. is_file – checks whether the specified path is a file.
$filename = "file.txt"; if (is_file($filename)) { echo "Is file!"; } else { echo "Not file!"; }
12. is_dir – checks if the specified path is a directory.
$dirname = "folder"; if (is_dir($dirname)) { echo "Its directory"; } else { echo "Its not directory"; }
13. filesize – returns the file size in bytes.
$filename = "file.txt"; $filesize = filesize($filename); echo "File size: " . $filesize . " bytes";
14. glob – finds files matching the specified pattern.
$files = glob("*.txt"); foreach ($files as $file) { echo $file; }
15. is_readable – checks whether the file is readable.
$filename = "file.txt"; if (is_readable($filename)) { echo "File can be readed"; } else { echo "Cant read the file"; }