PHP creates the file of folder and directory operation code

  • 2020-03-31 20:22:50
  • OfStack

One, directory operation
Opendir (),readdir(),closedir(), opendir(),readdir(), opendir()
 
<?php 
$base_dir="filelist/"; 
$fso=opendir($base_dir); 
echo $base_dir."<hr/>"; 
while($flist=readdir($fso)){ 
echo $flist."<br/>"; 
} 
closedir($fso) 
?> 

This is the program that returns the directory of the files under the directory of the files (the 0 file will return false).
Sometimes you need to know about the directory, so you can use dirname($path) and basename($path) to return the directory part of the path and the file name part, respectively. You can use disk_free_space($path) to return the rest of the view space.
Create command:
Mkdir ($path,0777) : 0777 is the permission code that can be set with the umask() function in non-windows.
Rmdir ($path) : files whose path is in $path will be deleted.
Two, file operation
low new file
First, determine the permissions of the directory where you want to create the file. 777 is recommended. Then, the absolute path is recommended for the name of the new file.
 
<?php 
$filename="test.txt"; 
$fp=fopen("$filename", "w+"); //Open the file pointer and create the file
if ( !is_writable($filename) ){ 
die(" file :" .$filename. " Can not write, please check! "); 
} 
//fwrite($filename, "anything you want to write to $filename."; 
fclose($fp); //Close the pointer

When reading the file
First is a file to see if it is readable (permission issue), or if it is, we can use the is_readable function to get information.
 
<?php 
$file = 'dirlist.php'; 
if (is_readable($file) == false) { 
die(' The file does not exist or cannot be read '); 
} else { 
echo ' There are '; 
} 
?> 

There is also file_exists to determine the existence of a file (shown below), but this is clearly not is_readable enough to be used when a file exists
 
<?php 
$file = "filelist.php"; 
if (file_exists($file) == false) { 
die(' File does not exist '); 
} 
$data = file_get_contents($file); 
echo htmlentities($data); 
?> 

However, the file_get_contents function is not supported in lower versions. You can first create a handle to the file and then read the whole file with a pointer:
There is another way to read binary files:
 
$data = implode('', file($file)); 

When writing files
Just like reading a file, see if you can write:
 
<?php 
$file = 'dirlist.php'; 
if (is_writable($file) == false) { 
die("You have no right to write!"); 
} 
?> 

You can use the file_put_contents function to write:
 
<?php 
$file = 'dirlist.php'; 
if (is_writable($file) == false) { 
die(' I'm a chicken feather , I can't '); 
} 
$data = ' I am a contemptible , I want to '; 
file_put_contents ($file, $data); 
?> 

The new file_put_contents function introduced in php5 (if you don't know it exists, use the function_exists function to determine if it exists) cannot be used in lower versions of PHP. You can use the following method:
 
$f = fopen($file, 'w'); 
fwrite($f, $data); 
fclose($f); 

Replace it.
Sometimes when writing a file, you need to lock it, and then write:
 
function cache_page($pageurl,$pagedata){ 
if(!$fso=fopen($pageurl,'w')){ 
$this->warns(' Unable to open cache file .');//trigger_error 
return false; 
} 
if(!flock($fso,LOCK_EX)){//LOCK_NB, exclusive locking
$this->warns(' Unable to lock the cache file .');//trigger_error 
return false; 
} 
if(!fwrite($fso,$pagedata)){//Write to the byte stream, and serialize writes to other formats
$this->warns(' Unable to write to cache file .');//trigger_error 
return false; 
} 
flock($fso,LOCK_UN);//Release the lock
fclose($fso); 
return true; 
} 

low copy and delete files
PHP file deletion is easy, simple operation with the unlink function:
 
<?php 
$file = 'dirlist.php'; 
$result = @unlink ($file); 
if ($result == false) { 
echo ' The mosquitoes drove away '; 
} else { 
echo ' Unable to get rid of '; 
} 
?> 

Can.
Copying files is also easy:
 
<?php 
$file = 'yang.txt'; 
$newfile = 'ji.txt'; #  The file parent folder must be able to write  
if (file_exists($file) == false) { 
die (' The demo is not up , Unable to copy '); 
} 
$result = copy($file, $newfile); 
if ($result == false) { 
echo ' Copy the memory ok'; 
} 
?> 

You can rename a folder using the rename() function; everything else is done by combining these functions.
low get file properties
Here are some common functions:
Get last modified time:
 
<?php 
$file = 'test.txt'; 
echo date('r', filemtime($file)); 
?> 

Returns a Unix timestamp, which is commonly used in caching techniques.
Related to this is the fileatime(),filectime() function that returns the fileowner when the file's permissions, owner, all groups, or metadata in other inodes are updated
$owner = posix_getpwuid (fileowner ($file));
(non-window system),ileperms(),
 
<?php 
$file = 'dirlist.php'; 
$perms = substr(sprintf('%o', fileperms($file)), -4); 
echo $perms; 
?> 

Filesize () returns the number of bytes in the filesize:
 
<?php 
//The output is like: somefile.txt: 1024 bytes
$filename = 'somefile.txt'; 
echo $filename . ': ' . filesize($filename) . ' bytes'; 
?> 

The stat() function returns an array to get all the information in the file:
 
<?php 
$file = 'dirlist.php'; 
$perms = stat($file); 
var_dump($perms); 
?> 

Related articles: