Introduction and examples of filesystem functions in php

Source: Internet
Author: User
Tags filegroup symlink
This article describes how to use filesystem functions in php. For more information, see

This article describes how to use filesystem functions in php. For more information, see

Basename-part of the file name in the returned 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 the correct suffix, 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-

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:
[1] =>
)

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.