Advanced PHP programming skills in Linux (2)

Source: Internet
Author: User
PHP advanced programming skills in Linux (2)-Linux general technology-Linux programming and kernel information. The following is a detailed description. 4. PHP and file system maintenance PHP has many file system-related functions. These functions can not only open files, but also display contents in directories, move files, and other functions, many people even use PHP to develop Internet-based file resource managers. File Path explanation: in Windows, you can use the/and symbol in the path, but in other operating systems, you can only use the/symbol. For the sake of consistency, we use the/symbol in a unified manner. The following script example shows a directory list with annotations included in the Code: ";/* Use a while statement to read all the elements in the opened directory. If the file name is not". "or"... ", the name */while ($ file_name = readdir ($ dir) {if ($ file_name! = ".") & ($ File_name! = "..") {$ File_list. ="
  • $ File_name ";}}$ file_list. ="";/* Close the opened directory and end the PHP module */closedir ($ dir);?>       Directory Listing         

    Files in:

              Now we have a directory list. Note that to read a file (which will be explained later) or directory, users on the PHP running system must have at least the permission to read the file. The following is an example of how to copy an object: This example is a prototype of a file backup system. When this script runs, it copies the file to a different location for saving. You can modify the daemon to execute it at the specified time of the day without user intervention. If you have installed Lynx on the system, you can create a daemon entry to access this file. Accessing this file will run this script and create a copy file, the following example runs the script at five o'clock A.M. and then closes Lynx: 0 5 *** [username] lynx-dump http: // localhost/copyfile. php 1>/dev/null 2> & 1 if you are running a CGI version of PHP, you can skip the Lynx section and directly call the binary file: 0 5 *** [username] php/path/to/copyfile. php 1>/dev/null 2> & 1 5. Abundant array functions PHP 4.0 adds 30 new functions related to the number of groups, some common functions can determine whether an array contains an element, count the elements in an array, add or delete the elements in the array, or sort the elements in the array. If there is a large array and you need to find out whether it contains a specific element, you can use in_array (). The following example shows "Not found in this array", because Albert is searched in an array named $ namesArray, and such an element does Not exist in the $ namesArray array. If You change the value of $ lookingFor to Mary, You will get "You 've got it !" Because Mary is an element in the $ namesArray array. To count the number of elements in an array, simply use the count () function: The returned $ count value is 7. You can add elements at the beginning or end of an array, or use array_merge () to create a new array containing two or more elements in the array, the order of elements is arranged in the specified order. If the original array is out of order, it needs to be re-ordered after merging. We can first use array_push () to add an element at the end of the array: ";}?> Run the above program and you will get the following result: 0: apple 1: orange 2: banana 3: kiwi 4: pear 5: grape 6: pineapple 7: if tomato needs to add elements at the beginning of the array, its code is similar to the above Code. The only difference is that array_unshift () must be used instead of array_push (). ";}?> Run the above program to get the following result: 0: grape 1: pineapple 2: tomato 3: apple 4: orange 5: banana 6: kiwi 7: pear array_merge () the function can combine two or more arrays into an array. ";}?> Run the above script to get the following result: 0: apple 1: orange 2: banana 3: kiwi 4: pear 5: carrot 6: green beans 7: asparagus 8: artichoke 9: now we know how to add elements and merge arrays. Let's take a look at how to delete elements from an array. You can use the array_pop () function to delete an element from the end of an array. The array_shift () function can be used to delete an element from the beginning of an array. Although an element is deleted from the Array Using array_pop () or array_shift (), you can use this element as a variable. Use array_pop () to delete an element from the end of an array: ";} Echo"
    And finally, in $ popped: $ popped ";?> Run the preceding script to obtain the following results: 0: apple 1: orange 2: banana 3: kiwi and finally, in $ popped: pear: Let's discuss another example of deleting an element from the end of an array: ";} Echo"
    And finally, in $ shifted: $ shifted ";?> Run the preceding script to obtain the following result: 0: orange 1: banana 2: kiwi 3: pear and finally, in $ shifted: apple also has several other functions to sort the elements in the array, but here we will only briefly introduce the basic sorting functions to illustrate the sorting process: "; <
  • 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.