How to create a directory in PHP (summary sharing)
在之前的文章《PHP中怎样获取目录中的文件名(总结分享)》中详细的介绍了PHP中应该怎样获取目录中文件名的相关知识,本篇文章我们还是来看一下PHP中目录处理的知识,关于怎样创建和删除目录。希望对大家有帮助!
在之前的文章中我们已经了解了怎样去打开关闭一个目录还有怎样去获取目录中的文件名,这些操作可以通过opendir()
函数、closedir()
函数、readdir()
函数和scandir()
函数来实现。想要使用这些函数,也就是函数想要执行成功的话,需要这个目标目录的存在。
这时候我们就应该要了解如果目标目录不存在,我们要怎样去创建一个目录,还有我们要怎样去删除一个目录。在PHP中我们有特定的函数能够创建目录,那就是mkdir()
函数,接下来就让我们一起来了解一下这个函数吧。
<strong><span style="font-size: 20px;">mkdir()</span></strong>
函数创建目录
在PHP中我们可以通过mkdir()
函数来进行创建目录的操作。
我们在很多的情况下都会需要新建一个目录来进行日常的开发,mkdir()
函数的基本语法格式如下:
mkdir(string $pathname[, int $mode = 0777[, bool $recursive = false[, resource $context]]])
其中需要注意的是:
mkdir()
函数运行成功的话,返回的值是true
;如果运行失败,返回的值是flase
。这么多的参数中$pathname
表示的是创建的这个目录保存的位置路径;$mode
这个参数它的默认值就是0777
,该参数表示的意思就是这个设定的目录权限,这个参数是由四个数组成的,默认的0777就是最大的访问权限了,如果运行的电脑系统是windows的话,$mode会被忽略的。
$recursive
是个可选参数,是用来设置递归模式的。$context
也是个可选参数,是用来规定文件句柄环境的。
我们继续来看一下$mode的设置问题,该参数是由四位数字构成的,每一位的数字分别有不同的含义,其中第一位数字通常都是0,剩下的三位数字都是用来规定权限的,他们规定的权限还不一样,第二位规定的是所有者,第三位规定的是所有者所属的用户组,第四位规定的是其他所有人。
接下来我们通过示例来看一下mkdir()函数的应用,示例如下:
<?php //规定指定路径 $dir = 'C:\Users\Administrator\Desktop\1.0\1103'; //判断该目录是否存在 if(is_dir($dir)){ echo "该目录存在!"; }else{ if(mkdir($dir,0777,true)) echo '目录不存在,并且目录创建成功!'; } ?>
上述例子我们想要实现通过mkdir()函数在“C:\Users\Administrator\Desktop\1.0\
”目录中再创建一个名为“1103
”的目录,输出结果如下:
并且在指定的位置就创建了一个名为“1103”的目录:
当然了我们也可以换一种方式,通过不同的路径表达方式也是能够创建一个目录的,示例如下:
<?php //规定指定路径 $dir = './test/demo'; //判断该目录是否存在 if(is_dir($dir)){ echo "该目录存在!"; }else{ if(mkdir($dir,0777,true)) echo '目录不存在,并且目录创建成功!'; } ?>
在没运行该代码之前,在本文件的同级目录中并没有test的目录也没有demo的目录:
当运行结束后,输出结果与上述示例结果相同,并且新建了文件:
当再次运行代码之后,输出结果为:
运行了代码之后就会在当前文件的同济目录中创建一个test的目录并且在test目录中再创建一个名为demo的目录。
其中我们还需要注意的是,在我们规定新的目录创建位置的时候,不能与已经存在的目录同名,如果同名的话程序户出现报错。
大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。
The above is the detailed content of How to create a directory in PHP (summary sharing). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op
