File operations
1. Only the server's files can be manipulated
2. file contains directories and files
Determine file type
Var_dump (FileType (". /0626 "));//Return file type directory is a dir file
Var_dump (Is_dir (". /0626 "));//Determine if the path is a directory
Var_dump (Is_file (". /ajax.php "));//Determine if the path is not a file
File properties
echo Date ("Y-m-d h:i:s", Fileatime (".. /ajax.php "));//Last Access time
echo Date ("Y-m-d h:i:s", Filectime (".. /ajax.php "));//Creation time
echo Date ("Y-m-d h:i:s", Filemtime (".. /ajax.php "));//Modification time
echo filesize (".. /ajax.php ");//File size byte units
Var_dump (File_exists (". /ajax.php "));//Determine if the file exists
File path
Current path./Parent Directory: /Subordinate directory directory/root Path/
If it is the root/path inside PHP: The root of the disk
If the root/path inside the page is represented by: server root www
Var_dump (file_exists ("/wamp/www/ajax.php"));
echo $_server[' document_root '];//fetch the root path of the server
Echo basename (".. /ajax.php ");//Get a filename with a suffix from the path
Echo basename (".. /ajax.php ",". php ");//Get a file name without a suffix from the path
Echo dirname (".. /ajax.php ");//directory to remove file names from the path
Var_dump (PathInfo (". /0630/upload.php "));//return directory information format to an array
Echo Realpath (".. /0630/upload.php ");//convert relative path to absolute path
Operating Directory
Var_dump (Glob (". /0627/*.php "));//Get all the files under the directory * *.php of a suffix
/* $attr =glob (".. /0627/* ");
for ($i =0; $i <count ($attr); $i + +)
{
echo $attr [$i]. " <br> ";
}*/
Get the number of files and folders under the directory use global variables as follows
/* $filenum = 0;
$dirnum = 0;
Num (".. /0627 ");
echo "Number of files is". $filenum. " <br> ";
The number of echo "Folders is". $dirnum;
function Num ($url)
{
Global $filenum;
Global $dirnum;
Determine whether the given path is not a file
if (Is_file ($url))
{
$filenum = 1;
return $filenum;
Exit
}
Get all the sub files in the directory
$attr =glob ($url. " /*");
Traverse all sub-files
foreach ($attr as $v)
{
if (Is_file ($v))
{
$filenum + +;
}
Else
{
$dirnum + +;
Num ($v);
}
}
echo "";
}*/
Only the number of files does not use global variables
/*echo Num (".. /0627 ");
function Num ($url)
{
$filenum = 0;
Determine whether the given path is not a file
if (Is_file ($url))
{
$filenum = 1;
return $filenum;
Exit
}
Get all the sub files in the directory
$attr =glob ($url. " /*");
Traverse all sub-files
foreach ($attr as $v)
{
if (Is_file ($v))
{
$filenum + +;
}
Else
{
$filenum = $filenum +num ($v);
}
}
return $filenum;
}*/
Calculate Folder Size
Echo dirsize (".. /0627 ")." <br> ";
function Dirsize ($url)
{
Define variable Store file size
$size = 0;
Determine whether the given path is not a file
if (Is_file ($url))
{
return FileSize ($url);
Exit
}
If it's all the files in the directory fetch directory
$attr =glob ($url. " /*");
Traversing sub-Files
foreach ($attr as $v)
{
if (Is_file ($v))
{
$size = $size +filesize ($v);
}
Else
{
$size = $size +dirsize ($v);
}
}
return $size;
}
Using Directory Resource traversal
$dir =opendir (". /0627 ");//Open Directory Resource
echo Readdir ($die);//read a file in the directory to perform a single read
while ($WJ =readdir ($dir))
{
echo $WJ. " <br> ";
if ($WJ = = "." | | $wj = = "..")
}
Closedir ($dir);//Close Directory Resource
Rewinddir ($dir);//Reset the pointer
Use Readdir () to read the first and second content
The first one is. Represents the current directory
The second is: Parent Directory
When using directory resources to traverse folders, be aware that the first two excluded if ($WJ = = "." | | $wj = = "..")
Directory Operations
1. Create a directory (folder)
MkDir ("./test");
2. Deleting a directory (folder) can only delete empty folders
RmDir ("./test");
3. Move directory (folder) <br/>
Rename ("./test", ".. /test11 ");
Overall file operation
Touch ("./test.txt");//Create File
Copy ("./test.txt", ".. /test.txt ");//Copy files
Unlink ("./test.docx");//delete file
Echo file_get_contents ("./test.txt");//Get File Content txt page
Echo file_get_contents ("http://www.baidu.com");//remote access to content
File_put_contents ("./test.txt", "Hello");//writing to a file will empty the original content
ReadFile ("./test.txt");//Get file contents and direct output
Var_dump (File ("./test.txt"));//reads the contents of the file to return an array, each element of the array corresponds to a row
File content Operations
1. Open a file resource
$FP =fopen ("./test.txt", "a");
Manipulating files
echo fgetc ($FP);//read one character at a time
/*while (!feof ($FP))//Determine if the file is read to the end
{
echo fgetc ($FP);
}*/
Echo fgets ($FP);//read one line at a time
Echo fread ($FP, 10);//reading a specific length line break 2 characters
Fwrite ($fp, "Hey Kaka \r\nok");//\ r \ n Line Change
2. Close File Resources
Fclose ($FP);
Delete a folder
<?php//Delete folder (including all files inside)// ./tp// ./tp/publicfunction Deldir ($dirname) {if (!file_exists ($dirname)) {die ("folder does not exist!");} If it is a file, delete it directly if (Is_file ($dirname)) {unlink ($dirname); exit ();} Open Directory Resource $dir = Opendir ($dirname), while ($filename = Readdir ($dir)) {if ($filename! = "." && $filename! = "...") {//******* $filename = $dirname. " /". $filename; if (Is_dir ($filename)) {Deldir ($filename);} else {unlink ($filename);}}} Closedir ($dir); rmdir ($dirname);} Deldir ("./tp");
The key points are as follows:
Var_dump (FileType (". /0529 ")); Returns the type of file: directory dir file
Var_dump (Is_dir (". /11.php ")); Determine if the path is a directory
Var_dump (Is_file (". /11.php ")); Determine if the path is not a file
echo filesize (".. /11.php "); File size, in bytes
Var_dump (File_exists (". /12.php ")); Determine if a file exists
Current path:./parent directory:. /Subordinate Directory: Directory/root path:/
If it is the root (/) path inside PHP: The root of the disk
If the root (/) path inside the Web page represents: root of the server (WWW directory)
Var_dump (PathInfo (". /0529/aa.php ")); Returns directory information, formatted as an array
Echo Realpath (".. /0529/aa.php "); Convert a relative path to an absolute path
Directory Resources
1. Create a directory (folder)
MkDir ("./test");
2. Deleting a directory (folder) can only delete empty folders
RmDir ("./test");
3. Move directory (folder)
Rename ("./test", ".. /test11 ");
Touch ("./test.txt");//Create File
Copy ("./test.txt", ".. /test.txt "); Copying files
Unlink ("./test.docx"); deleting files
Echo file_get_contents ("./test.txt"); Get file contents: TXT page
File Resources
PHP file Operations