Php and file operations

Source: Internet
Author: User

I. Directory operations

First, the functions read from the directory: opendir (), readdir (), closedir (). When used, the file handle is opened first, and then listed iteratively:
<? Php
$ Base_dir = "filelist /";
$ Fso = opendir ($ base_dir );
Echo $ base_dir. "While ($ flist = readdir ($ fso )){
Echo $ flist. "<br/> ";
}
Closedir ($ fso)
?>
This is a program that returns the files under the file directory that are already in the directory (0 files will return false ).

If you need to know the directory information, you can use dirname ($ path) and basename ($ path) to return the Directory and file name respectively. disk_free_space ($ path) is available) return to view the remaining space.

CREATE command:

Mkdir ($ path, 0777): 0777 is the permission code, which can be set by the umask () function in a non-window environment.

Rmdir ($ path): delete the file with the path in $ path.


Ii. File Operations


● Create a file

First, determine the permission for the directory where the file to be created is located. The recommended device is 777. We recommend that you use an absolute path for the new file name.
<? Php
$ Filename = "test.txt ";
$ Fp = fopen ("$ filename", "w +"); // open the file pointer and create a file
If (! Is_writable ($ filename )){
Die ("file:". $ filename. "cannot be written. Please check it! ");
}
// Fwrite ($ filename, "anything you want to write to $ filename .";
Fclose ($ fp); // close the pointer


● Read files

The first is to check whether a file can be read (permission problem) or whether the file exists. We can use the is_readable function to obtain information .:

<? Php
$ File = 'dirlist. php ';
If (is_readable ($ file) = false ){
Die ('file does not exist or cannot be read ');
} Else {
Echo 'exist ';
}
?>


The file_exists function is used to determine the existence of the file (as shown below), but this is obviously not comprehensive. It 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 earlier versions. You can create a handle for the file and then read all with pointers:

There is also a way to read binary files:

$ Data = implode ('', file ($ file ));

● Write files

The same way as reading files, let's see if it can be written:


<? Php
$ File = 'dirlist. php ';
If (is_writable ($ file) = false ){
Die ("You have no right to write! ");
}
?>


If you can write data, you can use the file_put_contents function to write data:

<? Php
$ File = 'dirlist. php ';
If (is_writable ($ file) = false ){
Die ('I am a chicken feather, I Can't ');
}
$ Data = 'I am cool, I want ';
File_put_contents ($ file, $ data );
?>

File_put_contents functions newly introduced functions in php5 (if you do not know the existence, use the function_exists function to judge first) are unavailable in lower versions of php. You can use the following method:


$ F = fopen ($ file, 'w ');
Fwrite ($ f, $ data );
Fclose ($ f );


Replaced.

When writing a file, you sometimes need to lock it, and then write:

Function cache_page ($ pageurl, $ pagedata ){
If (! $ Fso = fopen ($ pageurl, 'w ')){
$ This-> warns ('cache file cannot be opened. '); // trigger_error
Return false;
}
If (! Flock ($ fso, LOCK_EX) {// LOCK_NB, locking
$ This-> warns ('unable to lock the cache file. '); // trigger_error
Return false;
}
If (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats
$ This-> warns ('unable to write the cache file. '); // trigger_error
Return false;
}
Flock ($ fso, LOCK_UN); // release lock
Fclose ($ fso );
Return true;
}

● Copy and delete an object

Php is very easy to delete files. It is easy to use the unlink function:

<? Php
$ File = 'dirlist. php ';
$ Result = @ unlink ($ file );
If ($ result = false ){
Echo 'mosquito drive away ';
} Else {
Echo 'cannot get rid of it ';
}
?>

You can.

It is also easy to copy files:


<? Php
$ File = 'ang.txt ';
$ Newfile = 'ji.txt '; # The parent folder of this file must be writable.
If (file_exists ($ file) = false ){
Die ('small samples are not online and cannot be copied ');
}
$ Result = copy ($ file, $ newfile );
If ($ result = false ){
Echo 'Copy memory OK ';
}
?>

You can use the rename () function to rename a folder. Other operations are implemented by combining these functions.

● Get file attributes

I will talk about several common functions:
Obtain the last modification time:


<? Php
$ File = 'test.txt ';
Echo date ('R', filemtime ($ file ));
?>


The unix timestamp is returned, which is commonly used in cache technology.

Related to obtaining the last access time fileatime (), filectime () when the object's permission, owner, metadata in all groups or other inode is updated, fileowner () function return file owner
$ Owner = posix_getpwuid (fileowner ($ file ));
(Non-window system), ileperms () Get file permissions,

<? Php
$ File = 'dirlist. php ';
$ Perms = substr (sprintf ('% o', fileperms ($ file),-4 );
Echo $ perms;
?>

Filesize () returns the number of bytes of 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 a file, there is a function stat () that returns an array:

<? Php
$ File = 'dirlist. php ';
$ Perms = stat ($ file );
Var_dump ($ perms );
?>

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.