Simple interactive _php instance of PHP and server file system

Source: Internet
Author: User

1, php.ini on the file upload instructions

2, File upload process

(1) Upload file Submission Form HTML code:

<!--HTML form for uploading files to the server (restricted to text files)--> 
<! DOCTYPE html> 
 
 

(2) PHP processing upload file code

① in PHP script, the data that needs to be processed is stored in the $_files of the super variable array, 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 ']: stores the location where files are temporarily saved in the Web server;
$_files[' UserFile ' [' Name ']: stores the name of the file in the user's system;
$_files[' userfile ' [' Size ']: the size of the storage file;
$_files[' userfile ' [' type ']: The type of storage file;
$_files[' userfile ' [' ERROR]: Store any error codes associated with file uploads;

Error type Description:

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 upload 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 temporary folder could not 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 The upload file is included in the/uploads/directory on the server (the directory must be separate from the Web document tree) $upfile = '/uploads/'. $_files[' userfile ' [' name ']; Creates a new file under the specified directory with the file name of the transmission files if (Is_uploaded_file ($_files[' userfile '] [' tmp_name '])) {if (! Move_uploaded_file ($_files[') 
UserFile '] [' tmp_name '], $upfile)) {//move the temporary file of the transfer file to the new file created by Echo ' problem:could not moves file to destination directory '; 
Exit } else{echo ' problem:possible file upload attack. 
Filename: '; echo $_files[' uSername ' [' name ']; 
Exit 
echo "File uploaded seuucessfully<br/><br/>"; Clears HTML and PHP tags for transfer files $contents = file_get_contents ($upfile); Extracts the contents of a file into a string; $contents = Strip_tags ($contents); Erases the HTML and tags tags on the string; File_put_contents ($_files[' userfile '] [' name '], $contents); 
Re-write the string to the file;////Display the contents of the transferred text file on the browser echo "<p>preview of uploaded file Contents:<br/> 

3, the use of 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);//Open Directory, result returns a directory object 
echo " <p>upload directory is $current _dir</p> "; 
echo "<p>directory listing:</p><ul>"; 
while ($file = Readdir ($dir))!== false) {//Read directory object 
if ($file!= "." && $file!= ".") {//filter the current directory and the previous level directory 
echo "<li> $file </li>"; 
echo "<a href=\" filedetails.php?file=\. $file. \ ">". $file. " </a><br/> "; 
} 
} 
echo "</ul>"; 
Closedir ($dir); Close directory; 
?>

② uses PHP's dir class

<?php 
$current _dir = '/uploads/';//Create directory URL object 
$dir = Dir ($current _dir);//Create 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 under directory through the Dir object 
if ($file!= "." && $file!= ") { 
echo <li> $file </li>; 
} 
} 
echo "</ul>"; 
$dir->close (); Close directory 
?>

(2) using the Scandir () function to sort the alphabetical list of text names

<?php 
$current _dir = '/uploads/';//Create directory URL object 
$files 1 = scandir ($current _dir);//Save the file name under the specified directory as an array. The default is sorted alphabetically 
$files 2 = scandir ($current _dir,1);//Save the file name under the specified directory as an array, sorted by alphabetical order of 
echo "<p>upload directory is $ Current_dir</p> "; 
echo "<p>directory Listing in alphabetical order,ascending:</p><ul>"; 
foreach ($files 1 as $file 1) { 
if ($file 1!= "." && $file 1!= "...") 
echo "<li> $file 1</li>"; 
} 
echo "</ul>"; 
? >

(3) Get additional information about the current directory

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

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

Disk_free_space ($path): Returns the disk where the path can save the capacity of the uploaded file;

(4) Create and delete directories

①mkdir (): Create directory;

Code:

$oldumask = umask (0); Resets the 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, the interaction with the file system

(1) Obtain the file information:

while ($file = Readdir ($dir))!== false) {echo <a href=\ ' filedetails.php?file=\ '. $file. \ ' > '. $file. "
</a><br/> "; 
<?php $current _dir = '/uploads/'; $file = basename ($file); 
Get file File name echo " 

(2) Change file properties

CHGRP (File,group): Modify the grouping of files;

chmod (file,permissions): Permission to modify files;

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

(3) Create, delete, and move files

BOOL Touch ($filename, [int time,[int atime]]): Create a file (time specify create timestamp, atime specify optional timestamp)

Unlink ($filename): Delete a file

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

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

(4) Using program execution functions

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

Returns the last row of the named result, which can return the number of character groups, which represent each row of the output, Result_value get 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 done:

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 set to introduce the PHP and server file system simple interaction, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.