Introduction to filesystem functions in php and examples of use _ php instance

Source: Internet
Author: User
Tags filegroup symlink
This article describes how to use filesystem functions in php. For more information, see the file name section in the basename-return path.
Dirname-directory part of the returned path

The code is as follows:


String basename (string $ path [, string $ suffix])
String dirname (string $ path)

Example:

The code is as follows:


$ Path = "/home/httpd/phpha.com/index.php ";
Echo basename ($ path );
Echo basename ($ path, '. php ');
Echo basename ($ path, '. xxx ');
Echo dirname ($ path );
?>

The code is as follows:


// Result:
Index. php
Index
Index. php
/Home/httpd/phpha.com

Note: If the file name ends with a correct suffix file, this part will be removed.

Chgrp-change the group to which the file belongs
Chown-change the file owner
Chmod-change file mode

The code is as follows:


Bool chmod (string $ filename, int $ mode)

Example:

The code is as follows:


Chmod ('/home/phpha.txt', 0755 );
?>
Copy-copy an object

If (copy ('index. php', 'index. php. bak ')){
Echo 'copy success ';
}
?>
// The index. php. bak file is saved in the current directory.

Delete-see unlink or unset
Unlink-delete an object

The code is as follows:


If (unlink ('index. php. bak ')){
Echo 'unlink success ';
}
?>
// Index. php. bak deleted

Disk_free_space-return the available space in the directory
Disk_total_space-returns the total disk size of a directory
Diskfreespace-disk_free_space alias

The code is as follows:


// In Windows:
Echo disk_free_space ("C :"),'
';
Echo disk_total_space ("C :");
?>

The code is as follows:


// Result: the number of bytes is returned.
17433419776
32218386432

Fopen-open a file or URL
Fgets-read a row from the file pointer
Feof-test whether the file pointer is at the end of the file
Fread-read files (which can be safely used for binary files)
Fwrite-write a file (which can be safely used for binary files)
Fclose-close an opened file pointer

The code is as follows:


$ Fp = fopen('hello.txt ', 'r'); // open a file
$ N = 1;
While (! Feof ($ fp )){
Echo $ n, '-', fgets ($ fp ),'
'; // Read and output a row
$ N ++;
}
Fclose ($ fp); // close the file
?>

The code is as follows:


// Output:
1-Welcome to my blog:
2-http://www.jb51.net

Fgetc-read characters from the file pointer
Fgetcsv-read a row from the file pointer and parse the CSV field
Fgetss-read a row from the file pointer and filter out the HTML tag
Fputcsv-format rows as CSV and write file pointers
Fputs-fwrite alias

The code is as follows:


$ Fp = fopen('hello.txt ', 'r ');
While (false! ==( $ Char = fgetc ($ fp ))){
Echo $ char ,'-';
}
?>

The code is as follows:


// Output:
W-e-l-c-o-m-e--t-o--m-y-B-l-o-g -: ---h-t-p-:-/-B-l-o-g -. -p-h--. -c-o-m-

File_exists-check whether the file or directory exists

The code is as follows:


If(file_exists('hello.txt ')){
Echo 'hello.txt exists ';
} Else {
Echo 'hello.txt not exists ';
}
?>


[/Code]
// Output:
Hello.txt exists
[/Code]

File_get_contents-read the entire file into a string
File_put_contents-write a string to a file
File-read the entire file into an array

The code is as follows:


If ($ content = file_get_contents('hello.txt ')){
File_put_contents('hello.txt. bak ', $ content );
}
?>
// A Copy of hello.txt
If ($ content = file('hello.txt ')){
Print_r ($ content );
}
?>
// Array. each row is a group of several members.
Array
(
[0] => Welcome to my blog:
Http://www.jb51.net
)

Fileatime-the last time the file was accessed
Filectime-get the inode modification time of the file
Filegroup-the group for obtaining files
Fileinode-get the inode of the file
Filemtime-get file modification time
Fileowner-get the file owner
Fileperms-get file permissions
Filesize-get the file size
Filetype-get file type

The code is as follows:


Echo fileatime('hello.txt ');
Echo filectime('hello.txt ');
Echo filegroup('hello.txt ');
Echo filemtime('hello.txt ');
Echo fileowner('hello.txt ');
Echo substr (sprintf ('% O', fileperms('hello.txt'),-4 );
Echo filesize('hello.txt ');
Echo filetype('hello.txt ');
?>

The code is as follows:


// Output:
1353329003
1353329003
0
1353330002
0
0666
42
File

Flock-lightweight consultation file locking
Fnmatch-match the file name in the pattern
Fflush-outputs Buffered content to a file
Fpassthru-all the remaining data at the output file pointer
Fscanf-format input from file
Fseek-locate in file pointer
Fstat-get file information through the opened file pointer
Ftell-returned file pointer read/write location
Ftruncate-truncates a file to a specified length
Glob-find the file path that matches the pattern

Is_dir-determine whether the given file name is a directory
Is_executable-determine whether a given file name can be executed
Is_file-determine whether the given file name is a normal file
Is_link-determine whether a given file name is a symbolic connection
Is_readable-determine whether the given file name is readable
Is_uploaded_file-determines whether the file is uploaded through http post.
Is_writable-determine whether a given file name can be written
Is_writeable-is_writable alias
Note: the above functions are used to determine whether a file or directory meets the corresponding conditions. TRUE or FALSE is returned.

Lchgrp-Changes group ownership of symlink
Lchown-Changes user ownership of symlink
Link-create a hard connection
Linkinfo-get the connection information
Lstat-displays information about a file or symbolic connection.
Mkdir-create a directory
Move_uploaded_file-move the uploaded file to a new location
Parse_ini_file-parse a configuration file
Pathinfo-returned file path information
Pclose-disable process file pointer
Popen-open process file pointer
Readfile-output a file
Readlink-returns the target to which the symbolic connection points.
Realpath-returns the normalized absolute path name.
Rename-rename a file or directory
Rewind-position of the inverted file pointer
Rmdir-Delete directory
Set_file_buffer-stream_set_write_buffer alias
Stat-file information
Symlink-create a symbolic connection
Tempnam-create a file with a unique file name
Tmpfile-create a temporary file
Touch-set the file access and modification time
Umask-change the current umask
Clearstatcache-clear the file status cache

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.