PHP (PHP training PHP tutorial) programming common Skills Four
1. Configure the PHP file directory
As a scripting language for server-side interpretation, PHP is placed in a directory that can be accessed by a server, and can typically be configured by modifying Apache's Httpd.conj, such as our sentence in this file:
alias/test/"d:/phpwebsite/php/" then in the browser side input: "Http://localhost/test/+PHP file name" can access the PHP file under d:/phpwebsite/php/ The line is simply an alias for the directory where the php file is stored.
Second, you can also specify the DocumentRoot path to the directory where the server files, in Httpd.conj found the following two lines, where the path can be arbitrarily specified, your PHP files can be placed in the directory, the server can also run them, such as "http:// localhost/+php filename ".
In general, it is better to put PHP files in the same place, which can be conveniently managed, while other files are placed in another place.
DocumentRoot "c:/usr/bin/html/"
2. index file for server-side directory
In Httpd.conj, there is a line:
DirectoryIndex index.html
The row specifies that the directory default open file is Index.html, when a directory is accessed, the server will automatically find index.html, if not present, displays a list of all files in the directory, the default open file can be changed to another, such as index.php3 and so on, but perhaps we have a lot of directories, whether it is to store images, text and other materials, or to store PHP files or other files directory, we do not want users to see the list of files in the directory, in Httpd.conj A. htacess file is specified in the file that produces a directory index file, for example, we build a. htacess file with WordPad:
#. Htacess #
DirectoryIndex error_open.php
Then create an error warning file:
# error_open.php #
Where the error_open.php is a permission error warning file, put this 2 file in all protected directory, when the user attempts to open the directory, automatically turn to perform error_open.php, display error warning.
3. Directory Deletion ingenious implementation
We know that there is a rename () function in PHP4 For/win32 that can support renaming directories/files, such as:
Rename (OldPath, NewPath)//OldPath is the original path of the file or directory;
NewPath for the new defined path;
The implementation renamed OldPath to NewPath.
PHP4 not delete the directory/File function, how to delete it? We know that there is a line in php.ini that is used to complete the HTTP upload operation of the temporary file directory configuration lines:
upload_tmp_dir=;
PHP4 support the configuration of the temporary directory (PHP3 not supported), when the upload operation is completed automatically empty the temporary directory, well, with it we can skillfully implement file/directory deletion, such as settings: Upload_tmp_dir= "d:/phpwebsite/php/tmp/";
To delete a directory path, execute:
Tmp= "d:/phpwebsite/php/tmp/;"
Rename (path, TMP)
?>
After the file or directory path is renamed TMP, all files/directories under the TMP directory are automatically purged and the delete operation is completed.
4. Quickly build MySQL database tables
PHP and MySQL database to achieve a perfect combination, on the Web page, such as the Forum or the library published works of new users, we want to put his speech information online into the database, often in the corresponding database for the user to create a new data table. Win32 to create a new MySQL empty database is very simple, as long as in the "/mysql/data/" directory to create a folder, such as: "/usrinfo/", you can. Adding a new table to the library can be done in the following program:
# connect.mysql--Connect to Database #
Connection = mysql_connect ();
mysql_select_db ("Usrinfo", connection);
?>
# make.php--Create a data table with the following structure, with the name of the user named table #
Call Connect.mysql
Require ("Connect.mysql");
Check that a data table with a user name is a table name exists no?
query= "SELECT COUNT (*) from Usrname";
Result=mysql_db_query (query);
Does not exist then creates, if exists is the old user;
if (! Result) {
mysql_query ("
CREATE TABLE Usrname (
ID tinyint (6),
Title text,
Body Longtext,
Dateof date;
Timeof time;
) or Die (Mysql_error ());
}
This is where you insert a new part of the data into the data table
?>
Common Tips for PHP programming