The application arbitrarily processes the file and contains variable data that the user can enter, and carefully examines the user input to ensure that the user cannot perform any inappropriate actions on the file system. Listing 1 shows an example of a PHP site that downloads an image with the specified name.
- Php
- if ($_post[' submit '] = = ' Download ') {
- $ file = $_post[' fileName '];
- Header ("Content-type:application/x-octet-stream");
- Header ("Content-transfer-encoding:binary");
- header ("Content-disposition:attachment; filename = "". $file. "";" );
- $ FH = fopen ($file, ' R ');
- while (! feof ($FH))
- {
- Echo (Fread ($FH, 1024));
- }
- Fclose ($FH);
- } else {
- Echo ("<html><head>< ");
- Echo ("title>Guard your filesystem title> c19> head> ");
- Echo ("<body><form ID = "Myfrom" Action = "". $_server[' php_self '.
- "" Method = "POST" > ");
- Echo ("<Div><input type= "text" name= "FileName" value= "");
- Echo (isset ($_request[' filename '])? $_request[' filename ']: ');
- Echo ( " />");
- Echo ("<input type=" Submit " value = "Download" name= "Submit" /> Div > ");
- Echo (" form> body> html > ");
- }
As you can see, the more dangerous script in Listing 1 will handle all the files that the WEB server has read access to, including the files in the Session directory (see "Securing Session Data"), and even some system files (such as/etc/passwd). For a php-protected file system demonstration, this example uses a text box that allows the user to type a file name, but can easily provide a file name in the query string.
While configuring user input and file system access is risky, it is a good idea to design your application to use the database and hide the generated file names to avoid simultaneous configuration. However, this does not always work. Listing 2 provides a sample routine to verify the file name. It will use regular expressions to ensure that only valid characters are used in the file name, and specifically check for dot characters: ...
- function Isvalidfilename ($file) {
- /* don ' t allow. and allow any "word" character /* *
- Return Preg_match ('/^ (?:.) (?!.))| W) +$/', $file);
The above is the article for everyone to share the PHP protection file system specific code to write.
http://www.bkjia.com/PHPjc/446399.html www.bkjia.com true http://www.bkjia.com/PHPjc/446399.html techarticle file systems are very important to any site, and programmers are sparing no effort to protect their systems from being violated. Today, we will explain the PHP protection text for you ...