① In the php script, the data to be processed is stored in the super variable array $ _ FILES. Enabling the register_globals command can directly access this information through the variable name;
$ _ FILES ['userfile'] ['tmp _ name']: the temporary storage location of the stored FILES on the Web server;
$ _ FILES ['userfile'] ['name']: stores the name of a file in the user's system;
$ _ FILES ['userfile'] ['SIZE']: size of the stored file;
$ _ FILES ['userfile'] ['type']: type of the stored file;
$ _ FILES ['userfile'] ['error]: stores any error code related to file upload;
UPLOAD_ERR_INI_SIZE: 1. the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.
UPLOAD_ERR_FORM_SIZE: 2. The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
UPLOAD_ERR_PARTIAL: 3. only part of the file is uploaded.
UPLOAD_ERR_NO_FILE: 4. No file is uploaded.
UPLOAD_ERR_NO_TMP_DIR: 6. The Temporary Folder cannot be found. PHP 4.3.10 and PHP 5.0.3 are introduced.
UPLOAD_ERR_CANT_WRITE: 7. File writing failed. PHP 5.1.0 is introduced.
<? Php // verify if ($ _ FILES ['userfile'] ['error']> 0) {echo 'problem '; switch ($ _ FILES ['userfile'] ['error']) {case 1: echo 'file exceeded upload_max_filesize '; break; case 2: echo 'file exceeded max_file_size '; break; case 3: echo 'file only partially upload'; break; case 4: echo 'no File uploaded'; break; case 6: echo 'cannot upload file: no temp directory specified '; break; case 7: echo 'upload failed: Cann Ot write to disk'; break;} exit;} // check whether the transmitted file is a text file if ($ _ FILES ['userfile'] ['type']! = 'Text/plain ') {echo 'problem: file is not plain text'; exit ;} // include the uploaded file in the/uploads/directory on the server (this directory must be independent of the Web document tree) $ upfile = '/uploads /'. $ _ FILES ['userfile'] ['name']; // create a new file if (is_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name']) with the file name of the Transfer file in the specified directory) {if (! Move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'], $ upfile) {// move the temporary file for transfer to the created file echo 'problem: cocould not move file to destination directory '; exit;} else {echo 'problem: Possible file upload attack. filename: '; echo $ _ FILES ['username'] ['name']; exit;} echo "File uploaded seuucessfully <br/> "; // clear the html and php mark for the Transfer File $ contents = file_get_contents ($ upfile); // extract the file content into a string; $ contents = strip_tags ($ contents ); // erase the html and tags of the string; file_put_contents ($ _ FILES ['userfile'] ['name'], $ contents ); // re-write the string to the file; // display the transmitted text file content in the browser echo "<p> Preview of uploaded file contents: <br/>
3. Use directory Functions
(1) read the file name from the directory
① Use the opendir (), readdir (), closedir () functions;
<? Php $ current_dir = '/uploads/'; // create a directory url object $ dir = opendir ($ current_dir); // open the directory, returns a directory object echo "<p> Upload Directory is $ current_dir </p>"; echo "<p> directory Listing: </p> <ul> "; while ($ file = readdir ($ dir ))! = False) {// read the directory object if ($ file! = "." & $ File! = "..") {// Filter the current directory and the upper-level directory echo "<li> $ file </li>"; echo "<a href = \" filedetails. php? File = \'. $ file. \ '\ "> ". $ file. "</a> <br/>" ;}} echo "</ul>"; closedir ($ dir); // close the Directory;?>
② Use php's dir class
<? Php $ current_dir = '/uploads/'; // create a directory url object $ dir = dir ($ current_dir ); // create the dir object echo "<p> Handle is $ dir-> handle </p>"; echo "<p> Upload directory is $ current_dir </p> "; echo "<p> Directory Listing: </p> <ul>"; while ($ file = $ dir-> read ())! = False) {// read the file name in the directory through the dir object if ($ file! = "." & $ File! = ".. ") {Echo" <li> $ file </li> ";}} echo" </ul> "; $ dir-> close (); // close the directory?>
(2) Use the scandir () function to sort text names alphabetically
<? Php $ current_dir = '/uploads/'; // create a directory url object $ files1 = scandir ($ current_dir); // save the file name in the specified directory as an array, by default, $ files2 = scandir ($ current_dir, 1) is sorted in ascending alphabetical order. // save the file name in the specified directory as an array, echo "<p> Upload directory is $ current_dir </p>"; echo "<p> Directory Listing in alphabetical order, ascending: </p> <ul> "; foreach ($ files1 as $ file1) {if ($ file1! = "." & $ File1! = "...") Echo "<li> $ file1 </li>" ;}echo "</ul>" ;?>
(3) obtain other information about the current directory
Dirname ($ path): returns the directory part of the path;
Basename ($ path): return the name of the path;
Disk_free_space ($ path): the disk where the returned path is located can store the size of uploaded files;
(4) Create and delete directories
① Mkdir (): create a directory;
Code:
$ Oldumask = umask (0); // reset the current permission code mkdir ("/tmp/testing", 0777); // create the directory umask ($ oldumask ); // restore the current permission code
② Rmdir (): Delete the directory;
Code:
Rmdir ("/temp/testing"); or rmdir ("c: \ tmp \ testing ');
※The directory to be deleted must be empty;
4. Interaction with the file system
(1) Get File Information:
While ($ file = readdir ($ dir ))! = False) {echo "<a href = \" filedetails. php? File = \ '. $ file. \' \ ">". $ file. "</a> <br/>" ;}<? Php $ current_dir = '/uploads/'; $ file = basename ($ file); // obtain the file name echo "
(2) Modifying file Properties
Chgrp (file, group): modifies the file group;
Chmod (file, permissions): Modify the object ACL;
Chown (file, user): modifies the object owner;
(3) Creating, deleting, and Moving Files
Bool touch ($ filename, [int time, [int atime]): Creates a file (time specifies the creation timestamp, and atime specifies the optional timestamp)
Unlink ($ filename): deletes an object.
Copy ($ source_path, $ destination_path): copy an object
Rename ($ oldfile, $ newfile): rename an object;
(4) use a program to execute functions
① String exec (String command [, array result [, int result_value])
Return the last row of the naming result. The result variable can return the number of character groups. These strings represent each row of the output. result_value gets the return code;
② Void passthru (String command [, int result_value])
The result is directly output to the browser;
③ String system (string command [, int return_value])
Output the result to the browser and return the last line or false of the command result;
※The data submitted by the user includes part of the command execution, which should be packaged as follows:
System (escapeshellcmd ($ command ));
5. Interaction with Environment Variables
Phpinfo () function: obtains the list of all php variables;
Getenv ("$ key_name "):
Setenv ("$ key_name = $ value ");
The above is a simple interaction between PHP and the Server File System introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!