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

Source: Internet
Author: User
Tags echo date flock file permissions
First, directory operations
The first is the function read from the directory, Opendir (), Readdir (), Closedir (), which is used to open the file handle first and then iterate over the following list:
Copy Code code as follows:

<?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 in the directory below the file directory (0 files will return false).
Sometimes you need to know the contents of the directory, you can use DirName ($path) and basename ($path), respectively, to return the directory part of the path and the name of the filename section, available disk_free_space ($path) to return to view the space remaining space.
To create a command:
mkdir ($path, 0777): 0777 is the permission code, the Umask () function can be set under non window.
RmDir ($path): The file that will delete the path in $path.
Second, file operation
New file
First, determine which directory permissions you want to create a new file in; The recommended device is 777. The name of the new file is then recommended to use an absolute path.
Copy Code code as follows:

<?php
$filename = "Test.txt";
$FP =fopen ("$filename", "w+"); Open file pointer, create file
if (!is_writable ($filename)) {
Die ("File:". $filename. " Not written, please check! ");
}
Fwrite ($filename, "anything you want to write to $filename."
Fclose ($FP); Close pointer

Read files
First is a file to see whether can read (permission problem), or exist no, we can use the Is_readable function to get information.:
Copy Code code as follows:

<?php
$file = ' dirlist.php ';
if (is_readable ($file) = = False) {
Die (' File not present or unreadable ');
} else {
echo ' existence ';
}
?>

The function to determine the existence of a file is also file_exists (shown below), but this is clearly not is_readable comprehensive. When a file exists, you can use
Copy Code code as follows:

<?php
$file = "filelist.php";
if (file_exists ($file) = = False) {
Die (' File not present ');
}
$data = file_get_contents ($file);
echo htmlentities ($data);
?>

However, the file_get_contents function is not supported on a lower version, you can first create a handle to the file and then read it all with the pointer:
There is also a way to read binary files:
Copy Code code as follows:

$data = Implode ("", File ($file));

Write a file
and read the file in the same way, first see if you can write:
Copy Code code as follows:

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

If you can write it, you can use the File_put_contents function to write:
Copy Code code as follows:

<?php
$file = ' dirlist.php ';
if (is_writable ($file) = = False) {
Die (' I'm chicken feathers, I can't ');
}
$data = ' I am contemptible, I want ';
File_put_contents ($file, $data);
?>

The newly introduced function of the file_put_contents function in PHP5 (if you don't know it, use the Function_exists function to determine first) the lower version of PHP is not available, and you can use the following methods:
Copy Code code as follows:

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

Replaced.
When writing a file, you sometimes need to lock and then write:
Copy Code code as follows:

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 lock
$this->warns (' Unable to lock cache file. '); /trigger_error
return false;
}
if (!fwrite ($fso, $pagedata)) {//write byte stream, serialize write to other format
$this->warns (' Unable to write cache file. '); /trigger_error
return false;
}
Flock ($fso, Lock_un);/release lock
Fclose ($FSO);
return true;
}

copying, deleting files
PHP Delete file is very easy to use the unlink function simple operation:
Copy Code code as follows:

<?php
$file = ' dirlist.php ';
$result = @unlink ($file);
if ($result = = False) {
echo ' mosquito away ';
} else {
Echo ' cannot be driven away ';
}
?>

Can.
Copying files is also easy:
Copy Code code as follows:

<?php
$file = ' yang.txt ';
$newfile = ' ji.txt '; # This file parent folder must be able to write
if (file_exists ($file) = = False) {
Die (' sample not on-line, cannot copy ');
}
$result = Copy ($file, $newfile);
if ($result = = False) {
echo ' Copy memory OK ';
}
?>

You can use the rename () function to rename a folder. Other operations are a combination of these functions can be achieved.
Get file properties
I say a few common functions:
Get the last modified time:
Copy Code code as follows:

<?php
$file = ' test.txt ';
echo Date (' R ', Filemtime ($file));
?>

Returns the Unix-time timestamp, which is commonly used in caching techniques.
Related also to get last accessed time Fileatime (), Filectime () When file permissions, owner, all groups or other inode metadata is updated, Fileowner () function returns file owner
$owner = Posix_getpwuid (Fileowner ($file));
(Non-Window System), Ileperms () Gets the permissions of the file,
Copy Code code as follows:

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

FileSize () returns the number of bytes in the file size:
Copy Code code as follows:

<?php
Output similar to: somefile.txt:1024 bytes
$filename = ' somefile.txt ';
Echo $filename. ': ' . FileSize ($filename). ' bytes ';
?>

Get all the information for a file there is a function stat () function that returns an array:
Copy Code code as follows:

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