The php file operates on the instance code

  • 2020-05-17 04:52:24
  • OfStack

Let's start with a simple example
 
<?php 
if(!is_dir('txt'))// judge txt Is it a folder directory  
{ 
mkdir('txt');// Create a txt Folder directory  
$open=fopen('txt/in.txt',"w+");// Open the file as a read/write  
if(is_writable('txt/in.txt'))// If this file is writable  
{ 
if(fwrite($open," Today is a beautiful day 1 day ,1 Be happy! " - - " ")>0)// Write content  
fclose($open);// Close the file  
echo "<script>alert(' Write to successful ');</script>";// Output success prompt  
} 
} 
else 
{ 
if(is_file('txt/in.txt'))// Determine if the directory exists in.txt file  
{ 
if(is_readable('txt/in.txt'))// Determine if the file is readable  
{ 
echo file_get_contents('txt/in.txt');// Output text message  
unlink('txt/in.txt');// Delete the file in.txt 
rmdir('txt');// Delete the directory  
} 
} 
} 
?> 

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 need to pay attention to place. (this is not in php manual.)

2. Directory operation

opendir(),readdir(),closedir(), opendir(), 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 (0 file 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 to the view 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(); 
?> 

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

When reading the file

First is a file to see if it can be read (permission problem), or not, 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(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 read all of it with a pointer:

$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));

When writing 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 newly introduced file_put_contents function 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 that says unix, which is commonly used in caching techniques.

The fileowner() function returns the owner of the file when the permissions, owner, all groups, or other metadata in inode were updated

$owner = posix_getpwuid(fileowner($file));

(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. The 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 now it is very powerful.

Related articles: