PHP Basic Knowledge Summary (2) file Operation files

Source: Internet
Author: User
Tags configuration php fread glob http post rewind

First, the path

1, DirName--Returns the directory part of the path
$path = "/etc/passwd";
$file = DirName ($path); "/etc"

2, BaseName--Returns the file name part of the path
$path = "/home/httpd/html/index.php";
$file = basename ($path); index.php
$file = basename ($path, ". php"); Index

3, PathInfo--Returns the file path information in an array, including: dirname,basename and extension
$path _parts = PathInfo ("/www/htdocs/index.html");
echo $path _parts["DirName"]. "\ n";///www/htdocs
echo $path _parts["basename"]. "\ n";//Index.html
echo $path _parts["extension"]. "\ n";//html

4, Glob---Find the file path that matches the pattern, and return an array containing matching files/directories
foreach (Glob ("*.txt") as $filename) {
echo "$filename size". FileSize ($filename). "\ n";//funclist.txt size 44686
}
5, Realpath--Returns the normalized absolute path name

Ii. file Read and write

1, fopen--open file or URL
2, Fread-read the file, on the system to distinguish between binary files and text files (such as Windows) open the file, the fopen () function of the mode parameter to add ' B '.
3, Fwrite--write file,
4, Fclose--Close an open file pointer

$filename = "/usr/local/something.txt";
$handle = fopen ($filename, "RB");
$contents = Fread ($handle, FileSize ($filename));
Fwrite ($handle, 534);
Fclose ($handle);
' R ' is read-only, pointing the file pointer to the file header.
' r+ ' reads and writes, pointing the file pointer to the file header.
' W ' writes, points the file pointer to the file header and truncates the file size to zero
' w+ ' reads and writes, points the file pointer to the file header and truncates the file size to zero
' A ' writes, pointing the file pointer to the end of the file
' A + ' reads and writes, pointing the file pointer to the end of the file
' X ' is created and opened in writing, pointing the file pointer to the file header
' x+ ' is created and opened as read-write, pointing the file pointer to the file header

5. File--Reads the entire document into an array
$html = Implode (', File (' http://www.example.com/'));
6, file_get_contents--read the entire file into a string
7, File_put_contents (Filename,data[,flags[,context])--Writes a string to the file

Third, the document

1, file_exists-Check whether the file or directory exists
$filename = '/path/to/foo.txt ';
if (file_exists ($filename)) {}
2, Fileatime--Get the last access time of the file
Date ("F D Y h:i:s.", Fileatime ($filename));
3, Filectime--Returns the time when the file last Inode was modified, returned as a timestamp
Date ("F D Y h:i:s.", Filectime ($filename)
4, Filemtime--Get the file modification time
Date ("F D Y h:i:s.", Filemtime ($filename));
5, fileinode--returns the Inode node number of the file
6, filegroup--Get the group of files
7, Fileowner--Get the owner of the file
8, Fileperms--Get the file permissions
9, FileSize--Get the file size
10, filetype--Get the file type
11, ReadFile-read a file and write to the output buffer. Returns the number of bytes read from the file.
12, rename--rename a file or directory
13, unlink--delete the file "unset Delete variable"
14, Move_uploaded_file--Move the uploaded file to the new location flat [if the target file already exists, it will be overwritten]
15. Copy-copies the file from source to Dest. Returns TRUE if successful, FALSE if unsuccessful
$file = ' example.txt ';
$newfile = ' Example.txt.bak ';
if (!copy ($file, $newfile)) {echo "Failed to copy $file ... \ n";}

Iv. Catalogue

1, mkdir--New directory
2, RmDir-delete the directory, the directory must be empty, and to have the appropriate permissions.
3, ChDir--Change the directory
4, Chroot--Change the root directory
5, Closedir--Close the directory handle
6, GETCWD--Get the current working directory
7, Opendir--Open the directory handle
8. Readdir--Reading entries from the directory handle
9, Rewinddir--Reverse the directory handle
10, Disk_free_space--Returns the available space in the directory
$DF = Disk_free_space ("/");//$DF contains the number of bytes available in the root directory
Disk_free_space ("C:");//Under Windows
11, Disk_total_space--Returns the total disk size of a directory
$DF = Disk_total_space ("/");//$DF the size of the disk containing the "/" directory
Disk_total_space ("C:");//Under Windows
12, Scandir--Lists the files and directories in the specified path in an array


V. Judgment

1, Is_dir--to determine whether a given file name is a directory
2. Is_executable--Determine if the given file name is executable
3, Is_file--to determine whether the given file name is a normal file
4, Is_link--to determine whether the given file name is a symbolic connection
5, Is_readable--to determine whether the given file name is readable
6, Is_uploaded_file--Determine whether the file is uploaded via HTTP POST
7, Is_writable--to determine whether a given file name is writable

Vi.. Connection

1, link-establish a hard connection
2, LinkInfo--Get the information of a connection
3, Readlink--Returns the target to which the symbolic connection is directed
4, Symlink--Establish symbolic connection

Seven, the Hands

1, feof--Test whether the file pointer to the end of the file location
The file pointer must be valid and must point to a file that was successfully opened by fopen but not yet closed by fclose.
2. FGETC--reading characters from the file pointer
3. Fgetcsv--read a line from the file pointer and parse the CSV field
4, Fgets--Reads a line from the file pointer
5, FGETSS--Reads a line from the file pointer and filters out HTML tags
6, Fpassthru--all remaining data at the output file pointer
7, Fseek--positioning in the file pointer
8, Fstat---Get the file information through the open file pointer
9, Ftell--return the location of the file pointer read/write
10, Pclose--Close the process file pointer
11, Popen--Open process file pointer
12, rewind--rewind the position of the file pointer, set the handle file position pointer to the beginning of the file stream.


Viii. Other

1, fscanf---format the input from the file
2, Ftruncate--truncate the file to a given length
3, stat (filename)--Gets the statistics for the file specified by filename. If filename is a symbolic connection, the statistics are about the connection file itself.
Lstat--Give information about a file or symbolic connection

4, Tempnam--Create a file with a unique file name in the specified directory. If the directory does not exist, a file is generated in the system temp directory and its file name is returned.
$tmpfname = Tempnam ("/tmp", "FOO");
$handle = fopen ($tmpfname, "w");
Fwrite ($handle, "writing to Tempfile");
Fclose ($handle);
Do here something
Unlink ($tmpfname);

5, Tmpfile-in read-write (w+) mode to build a temporary file, the file will be closed (with fclose ()) or when the script ends automatically deleted.
$temp = Tmpfile ();
Fwrite ($temp, "writing to Tempfile");
Fseek ($temp, 0);
Echo fread ($temp, 1024);
Fclose ($temp); This removes the file

6, Touch (Filename[,time[,atime])--Set the file access and modification time
If time is empty, the current times are used. If you specify Atime, the access time for a given file is set to Atime. If the file does not exist, it will be created.

7, Umask-Change the current permissions, the PHP umask set to Mask & 0777 and return to the original umask

8, Fflush-forces all buffered output to be written to the resource pointed to by the handle file handle

9. parse_ini_file--parsing a configuration file

Nine, File upload

1, configuration PHP.ini

File_uploads = ON//whether to allow uploading of files over HTTP, by default allows
Upload_tmp_dir =//File upload temporary directory, if not specified use system default
Upload_max_filesize = 2M//maximum allowed upload file size, must be less than max_file_uploads
Max_file_uploads = 20
Max_execution_time = 30//upload time is 30s, over will automatically interrupt
Max_input_time = 60
; max_input_nesting_level=64
Memory_limit = 128M
post_max_size=; Sets the maximum amount of data allowed for post data, which must be greater than upload_max_filesize

2. Form: The submission method is Post,form must be added with the attribute enctype= "Multipart/form-data"

3. Get upload file properties

   (1) $_files[' filename ' [' Name ']      The original name of the client machine file
   (2) $_files[' filename ' [' type ']      file types
   (3 The size of the $_files[' filename ' [' Size ']      file
   (4) $_files[' filename ' [' Tmp_ The name ']  file is uploaded and then stored on the server temporary file name
   (5) $_files[' filename ' [' ERROR ']     wrong code   &NBSP
    0---file uploaded successfully
    1---uploading files over upload_max_ FileSize the value of the limit
    2---file uploads more than the max_file_size limit in the form
    3--- Files are only partially uploaded
    4---No files are uploaded
    6---Cannot find a temporary folder
     7---file write failed

 4, <input type= "hidden" name= "max_file_size" value= "30000";
          The hidden field must be placed before the file input field and its value is the maximum size of the accepted file, which is a recommendation to the browser, which PHP also checks to avoid the user taking the time to wait for the upload
         Large files after the file upload failed to find trouble
 5, Move_uploaded_file ()-- Move the uploaded file from the temp directory to the destination directory, which automatically checks to see if the file was uploaded via the HTTP post mechanism,
 6, Is_uploaded_file ()--to determine if the file was uploaded via HTTP post

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.