Advanced PHP programming skills in Linux (2)

Source: Internet
Author: User
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: Linux skills


   IV. PHP and file system maintenance
  
PHP has many functions related to the file system. 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:
  
   $ Dir_name = "/home/me /";
/* Create a handle whose value is the result of opening a given directory */
$ Dir = opendir ($ dir_name );
/* Create a text block to place the list element (file name )*/
$ File_list ="
    ";
    /* Use a while statement to read all the elements in the opened Directory. if the file name is not ".." or "..", the name in the list is displayed */
    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:
   $ Original = "/home/me/mydatabasedump ";
$ Copied = "/archive/mydatabasedumo_1010 ";
/* Use the copy () function to copy the original file. if the copy is not completed, an error message is displayed */
@ Copy ($ original, $ copied) or die ("Couldn't copy file .");
?>
  
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. Rich 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 and count the elements in an array, add or delete 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.
   $ LookingFor = "Albert ";
If (in_array ($ lookingFor, $ namesArray )){
Echo "You 've got it! ";
} Else {
Echo "Not found in this 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:
   $ Count = count ($ namesArray);?>
  
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:
   $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
/* Add elements to the array */
Array_push ($ fruitArray, "grape", "pineapple", "tomato ");
/* Display each element and its serial number */
While (list ($ key, $ value) = each ($ fruitArray )){
Echo "$ key: $ value
";
}
?>
  
Run the above program and you will get the following results:
0: apple
1: orange
2: banana
3: kiwi
4: pear
5: grape
6: pineapple
7: tomato
  
If you need to add elements at the beginning of the array, the code is similar to the above code. The only difference is that array_unshift () must be used instead of array_push ().
   /* Create an array */
$ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
/* Add elements to the array */
Array_unshift ($ fruitArray, "grape", "pineapple", "tomato ");
/* Display each element and its serial number */
While (list ($ key, $ value) = each ($ fruitArray )){
Echo "$ key: $ value
";
}
?>
  
Run the above program and you will get the following results:
0: grape
1: pineapple
2: tomato
3: apple
4: orange
5: banana
6: kiwi
7: pear
  
The array_merge () function combines two or more arrays into an array.
   $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
/*/Create the second array */
$ VegArray = array ("carrot", "green beans", "asparagus", "artichoke", "corn ");
/* Combine the two numbers into an array */
$ GoodfoodArray = array_merge ($ fruitArray, $ vegArray );
/* Display each element and its serial number */
While (list ($ key, $ value) = each ($ goodfoodArray )){
Echo "$ key: $ value
";
}
?>
  
Run the above script to get the following results:
0: apple
1: orange
2: banana
3: kiwi
4: pear
5: carrot
6: green beans
7: asparagus
8: artichoke
9: corn
  
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:
   /* Create an array */
$ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
/* Delete an element from the end of the array */
$ Popped = array_pop ($ fruitArray );
/* Display the content of the deleted array and the elements you deleted */
While (list ($ key, $ value) = each ($ fruitArray )){
Echo "$ key: $ value
";
}
Echo"
And finally, in $ popped: $ popped ";
?>
  
Run the above script to get the following results:
0: apple
1: orange
2: banana
3: kiwi
And finally, in $ popped: pear
  
Let's discuss an example of deleting an element from the end of an array:
   /* Create an array */
$ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
/* Delete an element from the beginning of an array */
$ Shifted = array_shift ($ fruitArray );
/* Display the content of the deleted array and the elements you deleted */
While (list ($ key, $ value) = each ($ fruitArray )){
Echo "$ key: $ value
";
}
Echo"
And finally, in $ shifted: $ shifted ";
?>
  
Run the preceding script and the following result is displayed:
0: orange
1: banana
2: kiwi
3: pear
And finally, in $ shifted: apple
  
There are also several functions that can sort the elements in the array, but here we will only briefly introduce the basic sorting function to explain the sorting process:
   $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
/* Sort the array */
Sort ($ fruitArray );
/* Display each element and its serial number */
While (list ($ key, $ value) = each ($ fruitArray )){
Echo "$ key: $ value
"; <

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.