Simple interactive _php instance of PHP and server file system

Source: Internet
Author: User
Tags html form string back
1. Setting instructions for file upload in php.ini

2. File Upload Process

(1) Upload file Submission Form HTML code:

 
      
 
  Adminstration-upoload New files   

Upload New files

(2) PHP processing upload file code

① in the PHP script, the data that needs to be processed is stored in the super variable array $_files, and the register_globals instruction can access the information directly through the variable name;

② If the form variable is named "username" there are:

$_files[' UserFile ' [' Tmp_name ']: Store files in a temporary storage location on the Web server;
$_files[' UserFile ' [' Name ']: stores the name of the file in the user's system;
$_files[' userfile ' [' Size ']: The size of the stored file;
$_files[' userfile ' [' type ']: The type of storage file;
$_files[' userfile ' [' ERROR]: Store any error code associated with the file upload;

Description of the error type:

Upload_err_ini_size:1, the uploaded file exceeds the value of the Upload_max_filesize option limit 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, the file is only partially uploaded.
Upload_err_no_file:4, no files were uploaded.
Upload_err_no_tmp_dir:6, the Temp folder cannot be found. PHP 4.3.10 and PHP 5.0.3 introduced.
Upload_err_cant_write:7, file write failed. PHP 5.1.0 introduced.

③php Code

<?php//Check File Transfer exception if ($_files[' userfile ' [' Error ']>0) {echo ' problem '; switch ($_files[' userfile ' [' Error ']) { Case 1:echo ' File exceeded upload_max_filesize '; Case 2:echo ' File exceeded max_file_size '; Case 3:echo ' File only partially upload '; Case 4:echo ' No file uploaded '; Case 6:echo ' cannot upload file:no temp directory specified '; Case 7:echo ' Upload failed:cannot write to disk '; } exit; }//Verify that the transferred file is a text file if ($_files[' userfile ' [' type ']! = ' Text/plain ') {echo ' problem:file is not plain text '; exit;}//Will upload the text /uploads/(the directory must be separate from the Web document tree) $upfile = '/uploads/'. $_files[' userfile ' [' name ']; Create a new file under the specified directory to transfer the file name if (Is_uploaded_file ($_files[' userfile ' [' tmp_name '])) {if (! Move_uploaded_file ($_files[') UserFile ' [' tmp_name '], $upfile)) {//Move temporary files for transfer files to a new file created echo ' problem:could not move file to destination directory '; exi T }} else{echo ' problem:possible file upload attack. Filename: '; echo $_files[' username ' [' name ']; Exit } EchO "File uploaded seuucessfully 

"; Clear HTML and PHP tags for transfer files $contents = file_get_contents ($upfile); Extracts the contents of the file into a string; $contents = Strip_tags ($contents); Erase HTML and tags tags on the string; File_put_contents ($_files[' userfile ' [' name '], $contents); Writes the string back to the file;//Displays the contents of the transmitted text file on the browser echo "

Preview of uploaded file contents:
> "; echo nl2br ($contents); echo "
";

3. Using Directory functions

(1) Read the file name from the directory

① uses Opendir (), Readdir (), Closedir () function;

<?php $current _dir = '/uploads/'; Create directory URL Object $dir = Opendir ($current _dir); Opens the table of contents and returns a directory object echo "

Upload Directory is $current _dir

"; echo "

Directory Listing:

    "; while (($file = Readdir ($dir))!== false) {//Read the directory object if ($file! = "." && $file! = "...") {//filter the current directory and the previous level of the directory echo "
  • $file
  • "; echo "". $file. "
    "; } } echo "
"; Closedir ($dir); Close a directory;?>

② using the Dir class of PHP

<?php $current _dir = '/uploads/'; Create directory URL Object $dir = Dir ($current _dir); Create the Dir object echo "

Handle is $dir->handle

"; echo "

Upload Directory is $current _dir

"; echo "

Directory Listing:

    "; while (($file = $dir->read ())!== false) {//Read the file name under directory through the Dir object if ($file! = "." && $file! = "...") {echo "
  • $file
  • "; } } echo "
"; $dir->close (); Close Directory?>

(2) Use the Scandir () function to sort the alphabet of text names

<?php $current _dir = '/uploads/'; Create a directory URL object $files 1 = scandir ($current _dir); Saves the file name under the specified directory as an array, sorted by default in ascending alphabetical order $files 2 = Scandir ($current _dir,1); Saves the file name under the specified directory as an array, sorted in descending alphabetical order echo "

Upload Directory is $current _dir

"; echo "

Directory Listing in alphabetical order,ascending:

    "; foreach ($files 1 as $file 1) {if ($file 1! = "." && $file 1! = ":") echo "
  • $file 1
  • "; } echo "
";?>

(3) Get additional information about the current directory

DirName ($path): Returns the directory portion of the path;

BaseName ($path): Returns the name part of the path;

Disk_free_space ($path): The disk where the return path is located can save the capacity of the uploaded file;

(4) Creating and deleting catalogs

①mkdir (): Create directory;

Code:

$oldumask = umask (0); Reset Current Permission Code mkdir ("/tmp/testing", 0777); Create directory Umask ($oldumask); Restore Current Permission Code

②rmdir (): Delete directory;

Code:

RmDir ("/temp/testing") or RmDir ("c:\\tmp\\testing");

※ The directory to be deleted must be an empty directory;

4. Interaction with the file system

(1) Get file information:

while (($file = Readdir ($dir))!== false) {echo "". $file. "
";} <?php $current _dir = '/uploads/'; $file = basename ($file); Get file filename echo "

Details of File

"; echo "

File data

"; Echo ' File last accessed: '. Date (' J F Y h:i ', Fileatime ($file)). "
"; Returns the timestamp of the most recently accessed Echo ' File last modifixed: '. Date (' J F Y h:i ', Filemtime ($file)). "
"; Returns the most recently modified timestamp $user = Posix_getpwuid (Fileowner ($file)); Returns the user ID uid echo ' File owner: '. $user [' name ']. "
"; $group = Posix_getgrgid (Filegroup ($file)); Returns the organization identity GID Echo ' File group: '. $group [' name ']. "
"; Echo ' File permissions: '. Decoct (Fileperms ($file)). "
"; Returns the 8-bit permission code echo ' File type '. FileType ($file). "
"; Return file type echo ' file Size '. FileSize ($file). "
"; Returns the number of file bytes echo "

File Tests

"; Echo ' Is_dir? '. (Is_dir ($file)? ' True ': ' false '). "
"; Echo ' Is_executable? '. (Is_executable ($file)? ' True ': ' false '). "
"; Determine if the file is executable; Echo ' Is_file? '. (Is_file ($file)? ' True ': ' false '). "
"; Echo ' Is_link? '. (Is_link ($file)? ' True ': ' false '). "
"; Echo ' Is_readable? '. (Is_readable ($file)? ' True ': ' false '). "
"; Echo ' Is_writable? '. (Is_writable ($file)? ' True ': ' false '). "
";?>

(2) changing file properties

CHGRP (file,group): Change the grouping of files;

chmod (file,permissions): Modify the permissions of the file;

Chown (File,user): Modify the owner of the file;

(3) Create, delete, and move files

BOOL Touch ($filename, [int time,[int atime]): Creates a file (time specifies the creation timestamp, atime specifies an optional timestamp)

Unlink ($filename): Delete a file

Copy ($source _path, $destination _path): Copy a file

Rename ($oldfile, $newfile): rename a file;

(4) Using program execution functions

①string exec (String command[, array result [, int result_value]])

Returns the last line of the named result, which can return the number of character groups that represent each line of the output, Result_value gets the return code;

②void PassThru (String command[, int result_value])

The result is output directly to the browser;

③string System (String command[, int return_value])

Outputs the result to the browser, returning the last line or false of the command result;

※ When the user submits the data including part of the execution order, the following packaging should be carried out:

System (Escapeshellcmd ($command));

5. Interacting with environment variables

Phpinfo () function: Gets a list of all the variables in PHP;

Getenv ("$key _name"):

Setenv ("$key _name= $value");

The above is a small part of the introduction of PHP and the server file system of simple interaction, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!

  • 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.