PHP Learning Basics-File System (i) file processing, file permissions

Source: Internet
Author: User
Tags pow
First, the PHP system file processing

/* PHP File System processing * All file processing is done using system functions. * is based on LINUX/UNIX system for Model * * File system processing function: * 1. All projects are inseparable from file processing * 2. You can save data for long periods of time with a file * 3. Establish the cache, the server file operation * * File processing * * *. File type * Linux-model, in Windows can only get file, dir or unknow three types * under Linux/unix, block, Char, dir, FIFO, file, link, unknown and type *block: Block settings files, disk partitions, floppy drives, CD-ROM, etc. *char: Character devices, I/O in characters, keyboards, printers, etc. *dir: Directories are also a *fifo of files: *file: *link: *unknown * * filetype ("directory or file Name ") * * Is_array (); * Is_int (); * Is_string (); * IS_NULL; * Is_bool (); * Is_dir--to determine whether a given file name is a directory is_executable--to determine whether a given file name can be executed Is_file--to determine whether a given file name is a normal file Is_link--to determine whether the given file name is a symbolic connection is_ Readable--to determine whether a given file name is readable Is_uploaded_file--to determine if a file is uploaded via HTTP POST is_writable--to determine if a given file name can be written is_writeable--is_writable ( ) is an alias * * * *. The properties of the file *file_exists (); *filesize (); *is_readable (); *is_writeable (); *filectime (); *filemtime (); *fileactime (); *stat (); * * *.  Functions related to file path * * Relative path: In relation to the ancestor and subordinate directories of the current directory *. Current directory *.. Previous level directory * *./php/apache/index.php *php/apahce/index.php *login.php *./login.php *. /images/tpl/logo.gif * * * path delimited symbol *linux/unix "/" *windows "\" * *directory_separator for different platforms in Windows \ Linux/* * No matter what operating system PHP directory split symbols are supported/(Linux) * in P                              In HP and Apache configuration files if you need to specify directories, also use/as directory symbols * * Absolute path: */root Path * */images/index.php * * refers to the root of the operating system * refers to the document root directory of the site * * Sub-condition * * If the path is executed in the server (via PHP file handler) the "root" refers to the root of the operating system * If the program is a downloaded client and then accesses the file on the server, only the APA Che access, "root" also refers to the document root directory * *http://www.xsphp.com/logo.gif * * *basename (URL) *dirname (URL) *pathinfo (URL) * * * * * *. File operations (create files, delete files, move files). Open and close the file (read the contents of the file, write to the file) *6. Move the pointer *7 inside the file. File locking some mechanisms for processing * * * Directory processing * 1. The traversal of the directory * 2. Creation of the directory * 3. Deletion of the directory * 4. The replication of the directory. Statistics Directory Size * * * File upload and download * 1. Upload * 2. Download * * * *


Second, PHP file attribute function instance

Date_default_timezone_set ("PRC"); function Getfilepro ($fileName) {if (!file_exists ($fileName)) {echo "file or directory {$ FileName} does not exist
"; return;} Else{echo "type of File". FileType ($fileName). "
";} if (Is_file ($fileName)) {echo "This is a file
The size of the "; echo" file is ". GetFileSize (FileSize ($fileName))."
";} if (Is_dir ($fileName)) {echo "This is a directory
";} if (is_readable ($fileName)) {echo "This file can be read
";} if (is_writable ($fileName)) {echo "This file can be written
";} if (is_executable ($fileName)) {echo "This file can be executed
";} echo "File creation time:". Date ("Y-m-d h:i:s", Filectime ($fileName)). "
Modified time for "; echo" File: ". Date (" Y-m-d h:i:s ", Filemtime ($fileName))."
Last access time for "; echo" File: ". Date (" Y-m-d h:i:s ", Fileatime ($fileName))."
";} function GetFileSize ($size) {$DW = "Byte", if ($size >= pow (2)) {$size =round ($size/pow (2, 2); $DW = "TB";} else if ($size >= pow (2)) {$size =round ($size/pow (2), 2); $DW = "GB";} else if ($size >= pow (2)) {$size =round ($size/pow (2), 2); $DW = "MB";} else if ($size >= pow (2)) {$size =round ($size/pow (2, Ten), 2); $DW = "KB";} else {$DW = "Bytes";} return $size. $dw;} Getfilepro ("Demo.txt"); Getfilepro ("Hello");


Third, PHP gets the file status function

Date_default_timezone_set ("PRC"); Echo '
';p Rint_r (stat ("Demo.txt")); Echo '
';


Iv. using File system cache data scheme

$cache =5;                   Cache time $cachefile= "Cache.txt";      Cached Files if (file_exists ($cachefile) && (Time ()-$cache) < Filemtime ($cachefile)) {   echo file_get_ Contents ($cachefile); If within 10 seconds of the cache, the data}else{file_put_contents ($cachefile, Date ("Y-m-d h:i:s", Time ()) is obtained from the cache file;}


Five, file path correlation function instance

$url 1= "./aaa/bbb/index.php"; Echo basename ($url 1). "
"; File name Echo dirname (dirname ($url 1)). "
"; Parent directory Echo dirname ($url 1). "
"; File directory echo '
';       File path information  print_r ($path =pathinfo ($url 3));  Echo '
'; echo $path ["extension"];

Vi. file system permissions-related function instances

  Create a file Touch ("filename") to  delete the file unlink ("File path");  Move the file to rename the file rename ("Current file path", "Directory for File Path") to  Copy the file ("current", "target");   Be sure to have PHP execute this file permission, Apache, a user    and permission design related function    ls-l  or ll  _rwxrwxrwx   777  _ type _ file  D means directory  l  B     rwx table The owner of this file  R Read W write x execute      rwx table The owner of this file the group  R Read W write x perform  rwx other users to this permission for the file  R read W write X perform r 4w 2x 1 7 7 7  4+2+1  4+2+1 4+2+1rwx   rwx  rwx6444+2   4   4rw_  r__ r__754  chmod u=rwx,g=rw,o=x  chmod 777  demo.php  chmod 644  demo.html  chown  MySQL demo.php  chgrp  Apache demo.php  chgrp--Change the group chmod the file belongs to--  change  The file mode chown--Change the file owner  filegroup-- Get the file group  Fileowner--Get the owner of the file
  • Related Article

    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.