PHP Create files (folders) and directory operation code _php tutorial

Source: Internet
Author: User
Tags echo date flock
First, directory operations
The first is the function that reads from the directory, Opendir (), Readdir (), Closedir (), which is used when the file handle is opened, and then the iteration is listed:
Copy CodeThe code is as follows:
$base _dir= "filelist/";
$fso =opendir ($base _dir);
echo $base _dir. " ";
while ($flist =readdir ($FSO)) {
echo $flist. "
";
}
Closedir ($FSO)
?>

This is the return file directory under which the file is already in the directory program (0 file will return false).
Sometimes you need to know the directory information, you can use DirName ($path) and basename ($path), respectively, return the directory portion of the path and the file name part, the Disk_free_space ($path) to return to view the space remaining space.
To create a command:
mkdir ($path, 0777): 0777 is the permission code, which can be set under non-window umask () function.
RmDir ($path): will delete the path in the $path file.
Second, the file operation
New file
First, determine the permissions of the directory where you want to create the new file; The recommended device is 777. Then, the name of the new file is recommended to use an absolute path.
Copy CodeThe code is as follows:
$filename = "Test.txt";
$FP =fopen ("$filename", "w+"); Open file pointer, create file
if (!is_writable ($filename)) {
Die ("File:". $filename. " Not writable, please check! ");
}
Fwrite ($filename, "anything you want-to-write to $filename.";
Fclose ($FP); Close pointer

Read the file
The first is a file to see if it can read (permission problems), or there is no, we can use the Is_readable function to obtain information.:
Copy CodeThe code is as follows:
$file = ' dirlist.php ';
if (is_readable ($file) = = False) {
Die (' file does not exist or cannot be read ');
} else {
echo ' presence ';
}
?>

The function of judging the existence of a file is also file_exists (shown below), but this is obviously not is_readable comprehensive. When a file exists, it can be used
Copy CodeThe code is as follows:
$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 on earlier versions, you can create a handle to the file first, and then read all with the pointer:
There is also a way to read a binary file:
Copy CodeThe code is as follows:
$data = Implode (', File ($file));

Write a file
and read the file the same way, first see if it can write:
Copy CodeThe code is as follows:
$file = ' dirlist.php ';
if (is_writable ($file) = = False) {
Die ("write!");
}
?>

If you can write it, you can use the File_put_contents function to write:
Copy CodeThe code is as follows:
$file = ' dirlist.php ';
if (is_writable ($file) = = False) {
Die (' I'm chicken feathers, I can't ');
}
$data = ' I am contemptible, I want to ';
File_put_contents ($file, $data);
?>

file_put_contents function in the php5 of the newly introduced function (do not know the existence of the function_exists function first to judge) the lower version of PHP is not available, you can use the following way:
Copy CodeThe code is as follows:
$f = fopen ($file, ' w ');
Fwrite ($f, $data);
Fclose ($f);

Replace it.
When writing a file, you sometimes need to lock and then write:
Copy CodeThe code is as follows:
function Cache_page ($pageurl, $pagedata) {
if (! $fso =fopen ($pageurl, ' W ')) {
$this->warns (' cannot open cache file. '); /trigger_error
return false;
}
if (!flock ($fso, lock_ex)) {//LOCK_NB, exclusive type lock
$this->warns (' cannot lock cache file. '); /trigger_error
return false;
}
if (!fwrite ($fso, $pagedata)) {//write byte stream, serialize write to other format
$this->warns (' cannot write to cache file. '); /trigger_error
return false;
}
Flock ($fso, lock_un);//Release lock
Fclose ($FSO);
return true;
}

Copy, delete files
PHP Delete Files is very easy, with the unlink function simple operation:
Copy CodeThe code is as follows:
$file = ' dirlist.php ';
$result = @unlink ($file);
if ($result = = False) {
Echo ' Mosquitoes have driven away ';
} else {
Echo ' cannot be driven away ';
}
?>

Can.
Copying files is also easy:
Copy CodeThe code is as follows:
$file = ' yang.txt ';
$newfile = ' ji.txt '; # The parent folder of this file must be able to write
if (file_exists ($file) = = False) {
Die (' sample not online, cannot copy ');
}
$result = Copy ($file, $newfile);
if ($result = = False) {
echo ' Copy memory OK ';
}
?>

You can use the rename () function to rename a folder. The other operations are done by combining these functions.
Get file properties
I say a few common functions:
Gets the last modified time:
Copy CodeThe code is as follows:
$file = ' test.txt ';
echo Date (' R ', Filemtime ($file));
?>

Returns a timestamp that says Unix, which is commonly used in caching techniques.
Related to the last time accessed Fileatime (), Filectime () when the permissions of the file, the owner, the metadata in all groups or other inode are updated, the Fileowner () function returns the file owner
$owner = Posix_getpwuid (Fileowner ($file));
(Non-Window System), Ileperms () Gets the file permissions,
Copy CodeThe code is as follows:
$file = ' dirlist.php ';
$perms = substr (sprintf ('%o ', Fileperms ($file)),-4);
Echo $perms;
?>

FileSize () returns the number of bytes in the file size:
Copy CodeThe code is as follows:
Output similar to: somefile.txt:1024 bytes
$filename = ' somefile.txt ';
Echo $filename. ': ' . FileSize ($filename). ' bytes ';
?>

To get all the information for a file, there is a function stat () function that returns an array:
Copy CodeThe code is as follows:
$file = ' dirlist.php ';
$perms = stat ($file);
Var_dump ($perms);
?>

http://www.bkjia.com/PHPjc/321381.html www.bkjia.com true http://www.bkjia.com/PHPjc/321381.html techarticle first, the directory operation is the function read from the directory, Opendir (), Readdir (), Closedir (), using the time is to open the file handle, and then iteration list: Copy code code is as follows: PHP $base ...

  • 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.