Read write and delete files in PHP operations on files and directories in of PHP

  • 2020-05-16 06:31:02
  • OfStack

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

This is to return the file directory under the file directory procedures (file 0 will return false).
Sometimes you need to know about the directory, you can use dirname($path) and basename($path) to return the directory part of the path and the name of the file, respectively. You can use disk_free_space($path) to return the view space free space.
Create command:
mkdir ($path, 0777)
,0777 is the permission code, which can be set by the umask() function under non-window.
rmdir ($path)
Will delete the path in the $path file.
The dir -- directory class is also an important class to manipulate the file directory. There are three methods,read,rewind,close. This is a pseudo-object-oriented class, which first opens the file handle and then reads it by pointer.
 
  <?php 
  $d = dir("/etc/php5"); 
  echo "Handle: " . $d->handle . "\n"; 
  echo "Path: " . $d->path . "\n"; 
  while (false !== ($entry = $d->read())) { 
  echo $entry."\n"; 
  } 
  $d->close(); 
  ?> 
[code] 
 The output : 
  Handle: Resource id #2 
  Path: /etc/php5 
  . 
  .. 
  apache 
  cgi 
  cli 
 The properties of the file are also important , File properties include creation time , Last modified time , The owner of the , File group , type , Size etc.  
 Let's focus on file manipulation . 
  2: File operations  
    Read the file  
 The first is 1 Let's see if we can read it ( Permission problems ), Or does it exist , We can use is_readable Function to get information .: 
[code] 
  <?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 the function file_exists(shown below), but this is obviously not as comprehensive as is_readable, which can 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 the lower version. You can first create a handle to the file and then use the pointer to read all of it:
 
  $fso = fopen($cacheFile, 'r'); 
  $data = fread($fso, filesize($cacheFile)); 
  fclose($fso); 

There is another way to read files in base 2:
$data = implode (' ', file ($file));
Learn in detail how to manipulate files and directories in PHP
An introduction to 1:
In any computer equipment, are necessary files are object, while in web programming, file action 1 straight is the place where web programmers have a headache, and, the operation of the file in cms system it is necessary and useful, we often encounter a file directory, file (folder) editing operations, such as I am now php instances of these functions to do a detailed summary and demonstrate how to use it. And about the corresponding functions in detail, please refer to php manual. Here only summarizes the key. And the need to pay attention to place. (this is not in php manual. )
2: directory operation
opendir(),readdir(),closedir(), opendir(), closedir(), opendir(), closedir()
 
<?php 
$base_dir = "filelist/"; 
$fso = opendir($base_dir); 
echo $base_dir."<hr/>" ; 
while($flist=readdir($fso)){ 
echo $flist."<br/>" ; 
} 
closedir($fso) 
?> 

This is to return the file directory under the file directory procedures (0 file will return false).
Sometimes you need to know the directory information, 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 to the view space free space.
Create command:
mkdir($path,0777),0777 is the permission code, which can be set by the umask() function under non-window.
rmdir($path) will delete the file whose path is in $path.
The dir -- directory class is also an important class to manipulate the file directory. There are three methods,read,rewind,close, which is a pseudo-object-oriented class. It first opens the file handle and then reads it by pointer.
 
<?php 
$d = dir("/etc/php5"); 
echo "Handle: " . $d->handle . "\n"; 
echo "Path: " . $d->path . "\n"; 
while (false !== ($entry = $d->read())) { 
echo $entry."\n"; 
} 
$d->close(); 
?> 

Output:
Handle: Resource id #2
Path: /etc/php5
.
..
apache
cgi
cli
The properties of the file are also very important, including the creation time, last modified time, owner, file group, type, size, and so on
Let's focus on file manipulation.
3: file operation
Read the file
The first one is a file to see if it can be read (permission problem), or not, we can use 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(shown below), but this is obviously not as comprehensive as is_readable, which can 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 the lower version. You can first create a handle to the file and then use the pointer to read all of it:
$fso = fopen($cacheFile, 'r');
$data = fread($fso, filesize($cacheFile));
fclose($fso);
There is another way to read files in base 2:
$data = implode('', file($file));
Write files
And the way to read a file 1, first to see if you can write:
 
<?php 
$file = 'dirlist.php'; 
if (is_writable($file) == false) { 
die(" I'm a chicken feather , I can't "); 
} 
?> 

If you can 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 function file_put_contents function introduced in php5 (if you do not know the existence of function_exists function, judge 1 first) cannot be used in the lower version of php. You can use the following method:
$f = fopen($file, 'w');
fwrite($f, $data);
fclose($f);
Replace it.
When writing a file, sometimes 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 cache file .');//trigger_error 
return false; 
} 
if(!fwrite($fso,$pagedata)){// Write byte stream ,serialize Write 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; 
} 

Copy and delete files
php delete files very easy, using the unlink function simple operation:
 
<?php 
$file = 'dirlist.php'; 
$result = @unlink ($file); 
if ($result == false) { 
echo ' The mosquitoes drove them 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 writable  
if (file_exists($file) == false) { 
die (' The demo is not online , 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 you can do with a combination of these functions.
Get file properties
Let me talk about some common functions:
Get the last modified time:
 
<?php 
$file = 'test.txt'; 
echo date('r', filemtime($file)); 
?> 

Returns a timestamp saying unix, which is commonly used in caching techniques.
The fileowner() function returns the file owner $owner = posix_getpwuid(fileowner($file)) when the file permissions, owner, all groups, or other metadata in inode were updated. (not window system),ileperms() access to files,
 
<?php 
$file = 'dirlist.php'; 
$perms = substr(sprintf('%o', fileperms($file)), -4); 
echo $perms; 
?> 

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

To get all the information of the file, there is a function stat() that returns an array:
 
<?php 
$file = 'dirlist.php'; 
$perms = stat($file); 
var_dump($perms); 
?> 

What does that key correspond to? I'm not going to expand it here.
4: conclusion
Above, I briefly summarized the following several file operations. If you have a good command of the functions listed above, there is no big problem in the operation. The function of php file operation changes quickly, and it is very powerful now.

Related articles: