PHP file and Directory operation example resolution

Source: Internet
Author: User
Tags php file

This article describes the PHP file and directory operations. Share to everyone for your reference, specific as follows:

File directory-related functions

<?php
Files in the output directory
Functionoutputcurfiles ($allowedtypes, $thedir) {
First, we make sure that the directory exists.
if (Is_dir ($thedir)) {
Now, we use Scandir to scan the files in the directory.
$scanarray = Scandir ($thedir);
Then we start parsing the array.
Scandir () with "." and ".." Statistics file navigation list
So as a document, we should not list them.
for ($i = 0; $i <count ($scanarray); $i + +) {
if ($scanarray [$i]!= "." && $scanarray [$i]!= "...") {
Now, check to make sure that this is a file, not a directory.
if (Is_file ($thedir.) /". $scanarray [$i])) {
Now, because we will allow the client to edit this file,
We have to check whether it is readable and writable.
if (Is_writable ($thedir.) /". $scanarray [$i]) && is_readable ($thedir." /". $scanarray [$i])) {
Now, we check whether the file type exists in the allowed array of types.
$thepath =pathinfo ($thedir.) /". $scanarray [$i]);
if (In_array ($thepath [' extension '], $allowedtypes)) {
If the file complies with the rules, we can continue to output.
echo$scanarray[$i]. " <br/> ";
}
}
}
}
}
}else{
echo "Sorry, this directory does not exist."
}
}
$allowedtypes =array ("TXT", "html");
Outputcurfiles ($allowedtypes, "TestFolder");
///////////////////////////////////////////////////
Functionrecurdir ($thedir) {
Attempt to open the directory.
try{
if ($adir = Opendir ($thedir)) {
Scan the directory.
while (false!== ($anitem = Readdir ($adir))) {
No statistics directory contains "." or ".." of the situation
if ($anitem!= "." && $anitem!= "...") {
At this point, if it's a directory, indent a little
And then go back to the recursive
if (Is_dir ($thedir.) /". $anitem)) {
? ><span style= "Font-weight:bold;" Mce_style= "Font-weight:bold;" ><?phpecho$anitem; ></span><?php
? ><div style= "margin-left:10px;" Mce_style= "margin-left:10px;" ><?php
Recurdir ($thedir. " /". $anitem);
? ></div><?php
}elseif (Is_file ($thedir.) /". $anitem)) {
The file is output at this time.
Echo$anitem. " <br/> ";
}
}
}
}else{
Thrownewexception ("Sorry, directory could not to be openend.");
}
}catch (exception$e) {
Echo$e->getmessage ();
}
}
echo "<br/>/////////////////////////////////////<br/><br/>";
Recurdir ("TestFolder");
//////////////////////////////////////////////////////////////////
echo "<br/>/////////////////////////////////////<br/><br/>";
Functionsortfilesbydate ($thedir) {
First, you need to make sure that the directory exists.
if (Is_dir ($thedir)) {
Next, we use Scandir to scan the files in this directory.
$scanarray = Scandir ($thedir);
$finalarray =array ();
Then start parsing the array
Scandir () with "." and ".." Statistics file navigation list
So as a document, we should not list them.
for ($i = 0; $i <count ($scanarray); $i + +) {
if ($scanarray [$i]!= "." && $scanarray [$i]!= "...") {
Now we check to make sure that this is a file and not a directory.
if (Is_file ($thedir.) /". $scanarray [$i])) {
What you need to do now is loop the data to an associative array.
$finalarray [$thedir.] /". $scanarray [$i]] =filemtime ($thedir." /". $scanarray [$i]);
}
}
}
Now that we've traversed the entire array, all we need to do is asort () it.
Asort ($finalarray);
return ($finalarray);
}else{
echo "Sorry, this directory does not exist."
}
}
We then point the function to the directory that we need to view.
$sortedarray = Sortfilesbydate ("TestFolder");
At this point, you can output as follows:
while ($element = each ($sortedarray)) {
echo "File:". $element [' key ']. "Is last modified:". Date ("F J, Y H:i:s", $element [' value ']). " <br/> ";
}
?>

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.