PHP Common file Operation function

Source: Internet
Author: User

1. BaseName () returns the file name portion of the path

$path = ' C:\AppServ\www\test\test.php ';
Echo basename ($path);

Results: test.php

2. DirName () returns the directory portion of the path

$path = ' C:\AppServ\www\test\test.php ';
echo dirname ($path);

Results: C:\AppServ\www\test

3. The file_get_contents (' text.txt ') function reads the entire file into a string.

4. File_put_contents (' Text.txt ', $str) function writes a string to the file.

5. The filesize (' Test.xml ') function returns the size of the specified file.
If successful, returns the number of bytes in the file size. If it fails, it returns false and generates an e_warning-level error.

6. The Disk_free_space (' C: ') function returns the free space in the directory in bytes.

7. The Disk_total_space (' C: ') function returns the total size of the disk in bytes for the specified directory.

8. fopen (' test.txt ', ' A + ') function to open the file or URL. If open fails, this function returns FALSE.
First parameter: The file to open. Second parameter: Open mode A + indicates read and write append mode open

9. The fwrite ($fp, $str) function writes to the file (which can be used safely in binary files).
The first parameter: the fopen () function returns the file handle. Second argument: the string to write.

The fclose ($fp) function closes an open file.

The unlink (' test.txt ') function deletes the file. If successful, returns True, which returns false if it fails.

The rmdir (' upload ') function removes the empty directory. If successful, the function returns True. If it fails, it returns false.

The Opendir () function opens a directory handle that can be used by Closedir (), Readdir (), and Rewinddir ().
If successful, the function returns a directory stream, otherwise it returns false and an error.

The Closedir () function closes the directory handle opened by the Opendir () function.

The Readdir () function returns an entry in a directory handle opened by Opendir ().
If successful, the function returns a file name, otherwise it returns false.

The Scandir () function returns an array that contains the files and directories in the specified path.

The Is_dir () function checks whether the specified file is a directory.

The file_exists () function checks whether a file or directory exists.
Returns true if the specified file or directory exists, otherwise false.

19. Use recursion to delete the directory and all files under the directory method.

function Deldir ($dir) {
@ $dh =opendir ($dir); Open Directory Stream
while ([email protected] ($DH)) {
if ($file! = '. ' && $file! = ' ... ') {
$fullpath = $dir. '/'. $file;
if (!is_dir ($fullpath)) {//If the file is deleted directly
Unlink ($fullpath);
}else{//If it is a directory recursive call itself
Deldir ($fullpath);
}
}
}
@closedir ($DH); Close Directory Stream
if (@rmdir ($dir)) {//delete directory itself
return true;
}else{
return false;
}
}

PHP Common file Operation function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.