


Operation examples of creating files (folders) and directories with PHP_PHP tutorial
Jul 21, 2016 pm 02:54 PM1. 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 :
<?php
$base_dir="liehuonet/";
$fso=opendir($base_dir);
echo $base_dir."<hr/>";
while($flist=readdir($fso)){
echo $flist."<br/>";
}
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.
<?php
$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.:
<?php
$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
<?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 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

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
