Php filter file path

Php filter file path

php:// — Accessing various I/O streams

Description

PHP provides a number of miscellaneous I/O streams that allow access to PHP’s own input and output streams, the standard input, output and error file descriptors, in-memory and disk-backed temporary file streams, and filters that can manipulate other file resources as they are read from and written to.

php://stdin, php://stdout and php://stderr

php://stdin , php://stdout and php://stderr allow direct access to the corresponding input or output stream of the PHP process. The stream references a duplicate file descriptor, so if you open php://stdin and later close it, you close only your copy of the descriptor-the actual stream referenced by STDIN is unaffected. It is recommended that you simply use the constants STDIN , STDOUT and STDERR instead of manually opening streams using these wrappers.

php://stdin is read-only, whereas php://stdout and php://stderr are write-only.

php://input

php://input is a read-only stream that allows you to read raw data from the request body. php://input is not available in POST requests with enctype=»multipart/form-data» if enable_post_data_reading option is enabled.

php://output

php://output is a write-only stream that allows you to write to the output buffer mechanism in the same way as print and echo .

php://fd

php://fd allows direct access to the given file descriptor. For example, php://fd/3 refers to file descriptor 3.

php://memory and php://temp

php://memory and php://temp are read-write streams that allow temporary data to be stored in a file-like wrapper. The only difference between the two is that php://memory will always store its data in memory, whereas php://temp will use a temporary file once the amount of data stored hits a predefined limit (the default is 2 MB). The location of this temporary file is determined in the same way as the sys_get_temp_dir() function.

The memory limit of php://temp can be controlled by appending /maxmemory:NN , where NN is the maximum amount of data to keep in memory before using a temporary file, in bytes.

php://filter

php://filter is a kind of meta-wrapper designed to permit the application of filters to a stream at the time of opening. This is useful with all-in-one file functions such as readfile() , file() , and file_get_contents() where there is otherwise no opportunity to apply a filter to the stream prior the contents being read.

The php://filter target takes the following parameters as part of its path. Multiple filter chains can be specified on one path. Please refer to the examples for specifics on using these parameters.

Читайте также:  Как использовать float в python

php://filter parameters

Name Description
resource= This parameter is required. It specifies the stream that you would like to filter.
read= This parameter is optional. One or more filter names can be provided here, separated by the pipe character ( | ).
write= This parameter is optional. One or more filter names can be provided here, separated by the pipe character ( | ).
Any filter lists which are not prefixed by read= or write= will be applied to both the read and write chains as appropriate.

Options

Wrapper Summary (for php://filter , refer to the summary of the wrapper being filtered)

Attribute Supported
Restricted by allow_url_fopen No
Restricted by allow_url_include php://input , php://stdin , php://memory and php://temp only.
Allows Reading php://stdin , php://input , php://fd , php://memory and php://temp only.
Allows Writing php://stdout , php://stderr , php://output , php://fd , php://memory and php://temp only.
Allows Appending php://stdout , php://stderr , php://output , php://fd , php://memory and php://temp only. (Equivalent to writing)
Allows Simultaneous Reading and Writing php://fd , php://memory and php://temp only.
Supports stat() No. However, php://memory and php://temp support fstat() .
Supports unlink() No
Supports rename() No
Supports mkdir() No
Supports rmdir() No
Supports stream_select() php://stdin , php://stdout , php://stderr , php://fd and php://temp only.

Examples

Example #1 php://temp/maxmemory

This optional parameter allows setting the memory limit before php://temp starts using a temporary file.

// Set the limit to 5 MB.
$fiveMBs = 5 * 1024 * 1024 ;
$fp = fopen ( «php://temp/maxmemory: $fiveMBs » , ‘r+’ );

// Read what we have written.
rewind ( $fp );
echo stream_get_contents ( $fp );
?>

Example #2 php://filter/resource=

This parameter must be located at the end of your php://filter specification and should point to the stream which you want filtered.

/* This is equivalent to simply:
readfile(«http://www.example.com»);
since no filters are actually specified */

Example #3 php://filter/read=

This parameter takes one or more filternames separated by the pipe character | .

/* This will output the contents of
www.example.com entirely in uppercase */
readfile ( «php://filter/read=string.toupper/resource=http://www.example.com» );

/* This will do the same as above
but will also ROT13 encode it */
readfile ( «php://filter/read=string.toupper|string.rot13/resource=http://www.example.com» );
?>

Example #4 php://filter/write=

This parameter takes one or more filternames separated by the pipe character | .

/* This will filter the string «Hello World»
through the rot13 filter, then write to
example.txt in the current directory */
file_put_contents ( «php://filter/write=string.rot13/resource=example.txt» , «Hello World» );
?>

Example #5 php://memory and php://temp are not reusable

php://memory and php://temp are not reusable, i.e. after the streams have been closed there is no way to refer to them again.

Источник

Search in directory to find files with certain file extensions

What is the best way to search through a directory using php and find out if the directory has any files that end with a specified file extension, like mp3 or pdf? I’m wondering if I can do this with an if statement.

3 Answers 3

foreach (glob("*.", GLOB_BRACE) as $filename)
◾GLOB_BRACE - Expands to match 'a', 'b', or 'c' 

Also take a look at PHP’s SPL DirectoryIterator.

At risk of over complicating a simple task, here is an OO SPL way of dealing with this.

_path = $path; $this->_extension = $extension; $this->_startWith = $startWith; $this->_endWith = $endWith; $it = $recursive ? new RecursiveDirectoryIterator($path, FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_SELF) : new DirectoryIterator($path); if($recursive) parent::__construct(new RecursiveIteratorIterator($it)); else parent::__construct($it); > /** * Filter function implemented for the FilterIterator * * @return bool */ public function accept() < $info = $this->getInnerIterator()->current(); if (!$info instanceof SplFileInfo) return false; if (!$info->isFile() || $info->isDot()) return false; $info = pathinfo($info->getFilename()); if($this->_extension[0] != null) _extension)) return false; > if($this->_startWith[0] != null) _startWith as $filter) < if(strncmp($file, $filter, strlen($filter)) == 0) < $found = true; break; >> if(! $found) return false; > if($this->_endWith[0] != null) _endWith as $filter) < if(substr_compare($file, $filter, -strlen($filter), strlen($filter)) === 0) < $found = true; break; >> if(! $found) return false; > return true; > > 
$fileScanner = new FileSearchIterator(path, true, array('mp3', 'pdf')); foreach($fileScanner as $file) < $file->//do something > 

Источник

PHP filtering files and paths according .gitignore

I want to use PHP to read all files and paths ignored by .gitignore configuration. Just like how git does. It’s possible to read directory repeatedly and use regular expression for each file to filter. But it`s so ineffective if the path have too much files. Any good and most effective way to read target files and path ignored by .gitignore?

Git provides the check-ignore subcommand that can tell you what paths are ignored (and, optionally, not ignored), and this of course handles the full complexity of the various ignore files and lists. My answer to another question provides the details of how to do this.

6 Answers 6

You need to proceed in several steps:

1 — Find the .gitignore files

Each folder can have one, so don’t assume there’s a single one.

And submodules have a .git link to the main .git folder, so be wary about stopping too early as well.

function find_gitignore_files($dir) < $files = array(); while (true) < $file = "$dir/.gitignore"; if (is_file($file)) $files[] = $file; if (is_dir("$dir/.git") && !is_link("$dir/.git")) break; # stop here if (dirname($dir) === '.') break; # and here $dir = dirname($dir); >return $files; > 

2 — Parse each .gitignore file

You need to ignore comments, mind the negation operator ( ! ), and mind the globs.

This one is, give or take, is going to go something like:

function parse_git_ignore_file($file) < # $file = '/absolute/path/to/.gitignore' $dir = dirname($file); $matches = array(); $lines = file($file); foreach ($lines as $line) < $line = trim($line); if ($line === '') continue; # empty line if (substr($line, 0, 1) == '#') continue; # a comment if (substr($line, 0, 1) == '!') < # negated glob $line = substr($line, 1); $files = array_diff(glob("$dir/*"), glob("$dir/$line")); >else < # normal glob $files = glob("$dir/$line"); >$matches = array_merge($matches, $files); > return $matches; > 

(Note: none of the above is tested, but they should put you in the right direction.)

This answer is incomplete and should not be marked as the accepted answer.. In details, PHP glob is functionally quite far from .gitignore patterns. Also you are not covering the listing of the files after applying filters.

Just a crazy idea: if you rely on Git to give you the patterns for ignored files why not rely on it to give the list of included/ignored files? Just issue a command like:

  • git ls-files for all tracked files
  • git clean -ndX or git ls-files -i —exclude-from=[Path_To_Your_Global].gitignore for all ignored files

See which Git command gives you the best output and then loop through the path files.

And a word of caution: take all the necessary precaution measures needed when executing external commands!

If that’s really the case, meaning you cannot issue a shell command, than you’re better of with the other solutions.

I use this function to read the Whole path, it works good

UPDATE: You Can Use preg_grep over the above function as follow

$files = preg_grep('~\.gitignore\b~i', array_values(read_dir($path))); 

entries in a .gitignore are mostly glob patterns. you can read each line of your .gitignore using php’s file function, ignore empty lines and lines that start with # and then read the patterns using the php glob function (http://php.net/manual/en/function.glob.php)

You can get an array of files to ignore from a .gitignore file and check against that. To do that, you would need to read the file and match files using the glob function.

First, get the contents of the file:

$contents = file_get_contents($pathToGitIgnoreFile); $path = dirname(realpath($pathToGitIgnoreFile)); 

You can also use the directory of the .gitignore file to match files in the same directory as the gitignore.

Next, we need to split the contents into individual rules. Rules start on their own line in the file. Lines that start with the pound symbol ( # ) are comments, so we can just use a regular expression to find non-blank lines that aren’t comments:

$rules = array(); preg_match_all('/[\\A\\v]([^#\\v]\\V*)[\\z\\v]?/', $contents, $rules); $rules = $rules[1]; 

Then all you have to do is iterate through the rules and use glob to create an array of file names to ignore:

$files = array(); foreach ($rules as $rule) < if (strpos($rule, '!') === 0) // negative rule $files = array_diff($files, glob($path . DIRECTORY_SEPARATOR . substr($rule, 1))); else $files = array_merge($files, glob($path . DIRECTORY_SEPARATOR . $rule)); >$files = array_unique($files); 

I didn’t test this code, so comment below if it doesn’t work for you.

Источник

Оцените статью