H5 Learning _ _php database operation

Source: Internet
Author: User
Tags file copy fread parent directory readable

1. File Operation 1.1 Open close file
    1. fopen ()

      resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )?

      The fopen () function binds resource to a stream or handle. After binding, the script can interact with the resource through a handle;

      Example 1: Open a text file that is located on the local server as read-only

      $fh = fopen("test.txt", "r");

      Example 2: Opening a remote file in read-only mode

      $fh = fopen("http://www.baidu.com", "r");

    2. Fclose ()

      bool fclose ( resource handle )

      Closes the file that the handle points to. Returns TRUE if successful, which returns FALSE if it fails;

      The file pointer must be valid and opened successfully through fopen () or fsockopen ();

      Although each request will eventually close the file automatically, it is a good practice to explicitly close all open files.

      Cases:

      $fhfopen("test.txt""r");fclose($fh);
1.2 Reading files

PHP provides many ways to read data from a file, not only one character at a time, but also the entire file at a time.

  1. Fread ()
    string fread ( int handle, int length )?
    The Fread () function reads a length character from the resource specified by the handle.

    The read stops when EOF is reached or when it is read to the length of a character.

    If you want to read the entire file, use the FileSize () function to determine the number of characters that should be read;

    Cases:

    $file"test.txt";$fhfopen$file"r");$strfread($fh, filesize($file$str;fclose($fh);
  2. Fgets ()
    string fgets ( int handle [, int length] )?
    The fgets () function reads a line of characters from the resource specified by handle. A newline character is encountered (included in the return value),

    EOF or has read the length-1 byte after the stop (see first encounter that kind of situation);

    Cases:

    Read a file line-wise

    $handle = fopen("data.txt""r");  while(!feof($handle)){             $content = fgets($handle);               $content= iconv(‘gbk‘,‘utf-8‘,$content);                      echo$content."<br />”;}   fclose($handle);

    Note: If length is not specified, the default is 1 K, or 1024 bytes.

  3. File ()
    array file ( string $filename [, int $flags = 0 [, resource $context ]])

    The file () function reads files into an array, separated by newline characters.
    Cases:

    $arrfile("test.txt");print_r($arr);
  4. File_get_contents ()

    string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )

    The file_get_contents () function reads the contents of the file into a string;
    Cases:

    $str = file_get_contents("test.txt");echo$str;
1.3 Writing Files
  1. Fwrite ()

    int fwrite (resource handle, string string [, int length])

    Fwrite () The handle function writes the contents of a string to the resource specified by the.

    If you specify a length parameter, the length character is written to stop.
    Example:

     $str  =  "Test text" ;  $fh  = fopen  (, " a "); fwrite  ( $fh ,  $str ); fclose  ( $fh );  
  2. File_put_contents ()

    int file_put_contents ( string filename, string data [, int flags [, resource context]] )

    The file_put_contents () function writes a string to a file, with the same function as calling fopen (), fwrite (), fclose () in turn;

    Cases:

    $str"hello";file_put_contents("test.txt"$str);
1.4 Copying, renaming, deleting files
    1. Copy ()

      bool copy ( string source, string dest )

      Copy the file from source to Dest. Returns TRUE if successful, and FALSE if it fails.

      Cases:Copy("test.txt", "test.txt.bak");

    2. Rename ()

      bool rename ( string oldname, string newname [, resource context] )

      Try renaming the oldname to NewName. Returns TRUE if successful, and FALSE if it fails.

      Cases:rename("test.txt", “test2.txt”);

    3. Unlink ()

      bool unlink ( string filename )

      Deletes the file if the delete succeeds returns True, otherwise returns false;

      Example 1:

      删除一个文本文件unlink(“test.txt");
1.5 reading a directory
  1. Copy ()

    bool copy ( string source, string dest )

    Copy the file from source to Dest. Returns TRUE if successful, and FALSE if it fails.

    Cases:Copy("test.txt", "test.txt.bak");

  2. Rename ()

    bool rename ( string oldname, string newname [, resource context] )

    Try renaming the oldname to NewName. Returns TRUE if successful, and FALSE if it fails.

    Cases:rename("test.txt", “test2.txt”);

  3. Unlink ()

    bool unlink ( string filename )

    Deletes the file if the delete succeeds returns True, otherwise returns false;

    Example 1:

    删除一个文本文件unlink(“test.txt");     
  4. Scandir ()

    array scandir ( string directory [, int sorting_order [, resource context]] )

    Returns an array containing the files and directories in the directory;

  5. RmDir ()

    bool rmdir ( string dirname )

    Delete Directory

  6. mkdir ()

    bool mkdir ( string pathname [, int mode [, bool recursive [, resource context]]] )
    ? Try creating a new directory that is specified by pathname.

1.6 Other file manipulation functions
    1. FileSize ()

      int filesize ( string filename )

      Gets the size of the file, in bytes

    2. Filectime ()

      int filectime ( string filename )

      Gets the creation time of the file, returned as a UNIX timestamp

      Cases:

      $t = filectime("test.txt");echo date("Y-m-d H:i:s"$t);
    3. Fileatime () returns the last changed time of the file;

    4. Filemtime () returns the last modified time of the file;

      Note: The last change time differs from the last modified time. The last change of time refers to any changes to the file inode data, including changing the permissions, the owning group, the owner, etc. And the last modification time refers to the modification of the contents of the file

    5. File_exists () checks if the file or directory exists and returns true if present, otherwise false;

    6. Is_readable () Determines whether the file is readable and returns true if the file exists and is readable;

    7. Is_writable () Determines whether the file is writable and returns True if the file exists and is writable;

1.7 Parsing directory path functions
    1. BaseName ()

      string basename ( string path [, string suffix] )

      Returns the file name part of the path, which is removed when an optional parameter is specified suffix;
      Cases:

2. Practice code in class
<?php        //Open File        $RH= fopen (' Php_3.txt ',' r+ ');//Read the file, the first parameter is the file handle, the second is the Read method        //Calculate file Size (bytes)        $num= FileSize (' Php_3.txt ');$str= Fread ($RH,$num);Echo $str;//If you set file access errors, you need to change the permissions of the file, properties--right down---open permissions- -to read writable        Echo ";//NewLine read recognition Enter not recognized <br>        $str _1= Fgets ($RH);$str _2= Fgets ($RH);//NewLine reads read again and continues to read the last read position        Echo $str _1;Echo ";Echo $str _2;//file Convert the contents of a file to an array,<br> directly into a newline, enter as a delimiter        $arr= File (' Php_3.txt '); Print_r ($arr);Echo ";//file_get_contents reads the contents of a file, returns a string, and can read external network data    //Echo file_get_contents (' php_3.txt ');        //directly read the site, stored in a text, you can directly get the other side of the page static layout, note, is static!     //$str _3 = file_get_contents (' http://www.lanou3g.com ');    //file_put_contents (' Php_3.txt ', $str _3);        //Rename    //rename (' Php_3.txt ', ' 1.txt ');    //rename (' 1.txt ', ' php_3.txt ');        //File copy used: /Override parent Folder    //Copy (' Php_3.txt ', '. /test.txt ');        //Read directory        //1. Open the file directory handle. (one point) to get this level of directory: (two points) is the parent directory        $RH _1= Opendir ('. ');//$arr = Readdir ()        //readdir Gets the file directory, which, like MySQL, must be removed using a loop         while($num= Readdir ($RH _1)) {//read out the            Echo $num;Echo "; }//Read directoryPrint_r (Scandir ('. '));//Create a new folder    //mkdir (' asdasd ');        //Delete entire folder Delete directory must ensure no other files inside the directory    //$is _bool = rmdir (' 1 ');        //Delete    //unlink (' php_3.txt ');        //Get File creation time        EchoFilectime (' Php_3.txt ');Echo ";//Returns the last time the file was accessed        EchoFileatime (' Php_3.txt ');Echo ";//Parse file specific name        EchoBaseName' Php_3.txt ',' txt ');Echo ";//Gets the name of the directory where the current file resides        EchoDirName' File/php_3.txt ');Echo ";//Return to the full, extended name, filenamePrint_r (PathInfo ("Php_3.txt"));//Modify file directory permissions        Echo "; Fclose$RH); Fclose$RH _1);?>

H5 Learning _ _php database operation

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.