Php file system details (recommended)

Source: Internet
Author: User
Tags php file upload
Php file system details (recommended)

  1. $ Filename = "./files. text ";
  2. $ Fp = fopen ($ finename, "rb ");
  3. $ Encho fread ($ fp, 100 );
  4. ?>

Readfile (), file () and file_get_contents () functions. readfile (), file () and file_get_contents () functions. int readfile (string filename [, bool use_include_path, resource context]); // reads a file and writes it to the buffer zone. if successful, the number of bytes is returned. otherwise, false is returned. Filename file name. The use_include_path parameter controls whether File Search in include_path is supported. true indicates yes. You do not need to open or close a file when using the readfile function. Array file (string filename [, bool use_include_path [, resource context]); // read the content of the entire file to the array. If the call succeeds, an array is returned. each element in the array is a line corresponding to the file and contains a line break. otherwise, false is returned. string file_get_contents (string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]); // The context value is the content added to 5.0, which can be ignored by NULL. Offset and maxlen is 5.1. Offset is used to mark the start position of a file, and maxlen sets the length of the file to be read. This method is applicable to reading binary files. Is the preferred method to read the file content into a string. If supported by the operating system, the memory ing technology will be used to enhance the performance. If you open a URL with special characters (such as spaces), use urlencode () for URL encoding.

Note: When readfile (), file (), and file_get_contents () are reading the content in the entire folder, the fopen () and fclose () functions are not required, but one character is being read, A line of characters and any length of characters must be used.

2. write file: int fwrite (resource handle, string [, int length]); // execute the file write operation. it also has an alias fputs (). this method is used to write the string content to the handle of the file pointer. If you set the length, the operation stops after writing length bytes or writing string. True is returned if the write operation is successful. otherwise, false is returned. Note: If the length parameter is given, the magic_quotes_runtime option in the php. ini file will be ignored, and the diagonal lines in the string will not be extracted. To distinguish between a binary file and a text file system, 'B' must be added to the mode parameter of the fopen () function when the file is opened '. Int file_put_contents (string filename, string data [. int flags [, resource context]); // write a string to the file. if the string is successful, the number of bytes is returned. otherwise, false is returned. Flags: to lock the file (the options include file_use_include_path, file_append: append, lock_ex: locked independently ). A context resource. Note: Although fwrite () can write files, it must support both fopen () and fclose () functions. File_put_contents () integrates the fopen (), fwrite (), and fclose () functions to write files separately. 3. if the file is closed, the function should be disabled. after the file operation is completed, the file should be closed; otherwise, an error may occur. Bool fclose (resouce handle); // close the file to which the handle parameter points. if the handle parameter is successful, true is returned. otherwise, false is returned.

When locking a file to write data to a text file, you must first lock the file to prevent other users from modifying the file content at the same time. Use the flock () function in php to lock files. Bool flock (int handle, int operation); // The operation parameter controls the lock permission. Including: lock_sh: Get Share Lock (read program ). Lock_ex: get the unique token lock (write ). Lock_un: release the lock. Lock_nb: Prevents The flock () function from being blocked during the lock.

The directory handler function directory is a special file. Since it is a file, if you operate it, you must first open it before you can browse it, and finally remember to close it.

1. open the Directory

Open the specified directory file. if the file is successfully opened, the directory handle is returned. Otherwise, false is returned. Unlike opening a file, if the directory does not exist, it does not automatically create a directory, but throws an error message. You can add the "@" symbol before the opendir () function to shield the output of error messages. Resource opendir (string path [, resource context]); // path specifies the directory file to be opened. If the path is not a valid directory or the file system cannot be opened due to permission issues, the function returns false and generates an E_WARNING error message.

2. Browse directory

Use the handle returned by the opendir function and the scandir function for browsing. Array scandir (string directory [, int sorting_ordering [, resource context]); // used to browse directories and files in a specified path. If the operation succeeds, an array containing the file name is returned. otherwise, false is returned. Directory specifies the directory to be browsed. if it is not a directory, false is returned and an error message at the E_WARNING level is generated. Sorting_order is used to set the sort order. by default, the sort order is ascending. If this parameter is set, it is sorted in descending order. Note: The is_dir () function determines whether the specified file name is a directory. If the file name exists and is a directory, true is returned; otherwise, false is returned. If it is a relative directory, check its relative path according to the current working directory.

3. close the directory. Void closedir (resource handle); // handle, the handle of the working directory to be closed.

Note: We have learned that if the opened directory does not exist, the system will not create a directory for us. Then we can create the desired directory on our own. You can use the mkdir () function to create a new directory. if the directory is created successfully, true is returned. otherwise, false is returned. Rmdir () function: Delete a directory. The directory must be empty (the directory does not contain files or subdirectories) and must have operation permissions. Unlink () function: delete an object. if the object is successfully deleted, true is returned. if the object fails, false is returned.

Principles of PHP file upload and php file downloadStep 1: control the file upload and configure it through the php. ini file. Step 2: determine the uploaded file. The size and format of the uploaded file. Step 3: perform file upload operations.

1. Control file upload:

Php controls the file to be uploaded by using php. ini, including: whether the file can be uploaded, the temporary directory of the file to be uploaded, the size of the file to be uploaded, the execution time of the command, and the memory space allocated by the command. Locate the file uploads option in the php. ini file to complete the settings of the above options. The option description is as follows: file_uploads: if it is on, the server supports file upload. if it is off, it does not. This option is generally supported by default and does not need to be modified. Upload_tem_dir: temporary directory of the uploaded file. Before the file is uploaded successfully, the file is saved in the temporary directory of the server. Most of them use the default system directory, but you can also set it yourself. Upload_max_filesize: maximum size of files that can be uploaded by the server, in MB. The default value is 2 MB. if the value is exceeded, modify the value. Max_execution_time: maximum time for executing a command in php, in seconds. This command must be modified when uploading a large file. Otherwise, the file will be uploaded within the permitted range of the server in time. However, if the maximum time allowed by the command is exceeded, the file cannot be uploaded. Memory_limit: memory space allocated by a command in php, in MB. The size of the file also affects the upload of large files.

Note: The enctype and method attributes in the form are controlled when the file is uploaded to the application on the client, and the MAX_FILE_SIZE. enctype = "multipart/form-data" field is hidden: specifies the form encoding data mode. Method = "post": specifies the data transmission mode.: Controls the size of the uploaded file by hiding the domain, in bytes. The value cannot exceed the value set by the upload_max_filesize option in the php. ini configuration file. It cannot completely control the size of the uploaded file, but can only avoid unnecessary troubles.

2. determine the file to be uploaded

The global variable $ _ FILES and $ _ FILES are used to determine the uploaded FILES. they are an array that contains information about all uploaded FILES. The meaning of each element in the array is as follows: $ _ FILES [filename] [name]: the name of the file stored on the file, such as text.txtand title.jpg. $ _ FILES [filename] [size]: the size of the stored file, in bytes. $ _ FILES [filename] [tem_name]: name of the file used to store the file in the temporary directory, because the file must be stored in the temporary directory as a temporary file before being uploaded. $ _ FILES [filename] [type]: the MIME type used to store uploaded FILES. MIME specifies the types of various file formats. each MIME type is composed of the primary and child types separated. For example, the main type of "image/gif" is image, and the child type is GIF file. "Text/html" indicates the HTML file of the text. $ _ FILES [filename] [error]: error code for storing file uploads: This project is added to PHP4.2.0. The return value is composed of five types: 0: indicating no error. The file is uploaded successfully. 1: indicates that the size of the uploaded file exceeds the limit of the upload_max_filesize option in the configuration file command. 2: The size of the uploaded file exceeds the value specified by the max_file_size option in the HTML form. 3: indicates that only part of the file is uploaded. 4: indicates that no files have been uploaded. Example:

  1. /* Determine whether an image is uploaded */
  2. If (! Empty ($ _ FILES ['up _ picture '] [name]) {
  3. /* Assign the image information to the variable */
  4. $ Type = strtolower (strstr ($ _ FILES ['up _ picture '] [name], ".");
  5. If ($ type! = '.Jpg '& $ type! = '.Gif ') echo "the format of the file you uploaded is incorrect ";
  6. Else {
  7. If ($ _ FILES ['up _ picture '] [size] <2000000 & $ _ FILES ['up _ picture'] [size]> 0 ){
  8. Echo "Upload file name:". $ _ FILES ['up _ picture '] [name]."
    ";
  9. Echo "Upload file type:". $ type ."
    ";
  10. Echo "Upload file size:". $ _ FILES ['up _ picture '] [size]."
    ";
  11. } Else echo "the image size does not meet the requirements. ";
  12. }
  13. }
  14. ?>

12. Next page

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.