Home  >  Article  >  Backend Development  >  Operation examples of creating files (folders) and directories with PHP_PHP tutorial

Operation examples of creating files (folders) and directories with PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:54:581677browse

1. Directory operation

The first is the function to read from the directory, opendir(), readdir(), closedir(). When used, the file handle is opened first, and then iteratively listed :
$base_dir="liehuonet/";
$fso=opendir($base_dir);
echo $base_dir."


";
while($flist=readdir($fso)){
echo $flist."
";
}
closedir($fso)
?>
this It is a program that returns the files and directories under the file directory (0 files will return false).

Sometimes you need to know the directory information, you can use dirname($path) and basename($path) to return respectively For the directory part and file name part of the path, disk_free_space($path) can be used to return the remaining space of the view space.

Creation command:

mkdir($path,0777): 0777 is the permission code, It can be set with the umask() function under non-window conditions.

rmdir($path): will delete files with paths in $path.

2. File operations

● New File

First, determine the permissions of the directory where the file you want to create is located; the recommended device is 777. Then, it is recommended to use an absolute path for the name of the new file.
$filename="test.txt";
$fp=fopen("$filename", "w+"); //Open the file pointer and create the file
if ( !is_writable($filename) ){
die("File:" .$filename. "Not writable, please check!");
}
//fwrite($filename, "anything you want to write to $filename.";
fclose($fp); //Close the pointer

● Read the file

First, check whether a file can be read (permission issue), or Yes, we can use the is_readable function to get the information.:

$file = 'dirlist.php';
if (is_readable($file) == false) {
die('The file does not exist or cannot be read');
} else {
echo 'exists';
}
?>

Function to determine the existence of the file There is also file_exists (demonstrated below), but this is obviously not as comprehensive as is_readable. When a file exists, you can use

$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 lower versions. You can first create a handle to the file, and then use the pointer to read all:

  • Total 3 pages:
  • Previous page
  • 1
  • 2
  • 3
  • Next page

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364471.htmlTechArticle1. Directory operations are first functions to read from the directory, opendir(), readdir(), closedir() , when using it, open the file handle first, and then list it iteratively: ?php $base_dir=liehuonet/; $fso=open...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn