How to create a directory on ftp and set permissions in php

PHPz
Release: 2023-04-21 09:29:37
Original
709 people have browsed it

PHP is a popular programming language that is used to develop many web applications. One of the common applications is the File Transfer Protocol (FTP) client. In PHP, we can use FTP functions to establish a connection to an FTP server and perform many FTP operations. This article explains how to use FTP functions in PHP code to create directories and set permissions.

  1. Establish FTP connection

Before using the FTP function, we must first establish a connection with the FTP server. We can use the "ftp_connect" function to establish a connection. This function requires us to provide the address and port number of the FTP server. After the connection is successful, we need to use the "ftp_login" function for login verification. This function requires us to provide a username and password.

Here is a sample code to establish an FTP connection:

// 设置 FTP 服务器地址和端口号
$ftp_server = "ftp.example.com:21";

// 设置 FTP 用户名和密码
$ftp_username = "username";
$ftp_password = "password";

// 建立 FTP 连接
$conn = ftp_connect($ftp_server);

// 登录验证
$login = ftp_login($conn, $ftp_username, $ftp_password);

// 检查连接和登录是否成功
if (!$conn || !$login) {
   die("FTP 连接失败");
}
Copy after login
  1. Create Directory

Once we have established a connection to the FTP server, we can Use the "ftp_mkdir" function to create the directory. This function requires us to provide the name of the directory and the path where the directory is located. As shown below:

// 设置目录名称和路径
$dir_name = "new_dir";
$dir_path = "/public_html/";

// 创建目录
if (ftp_mkdir($conn, $dir_path . $dir_name)) {
   echo "目录创建成功";
} else {
   echo "目录创建失败";
}
Copy after login

In the above code, we use the "ftp_mkdir" function to create a directory named "new_dir". We use the path "/public_html/" to specify where on the FTP server the directory is created. If the "ftp_mkdir" function returns true, the directory creation was successful. Otherwise, the creation fails.

  1. Set directory permissions

On the FTP server, each file and directory has its own permissions. We can use the "ftp_chmod" function to set the permissions of a directory.

Permissions are represented by numbers, where each number represents a different permission. The first number indicates owner permissions, the second number indicates group permissions, and the third number indicates other user permissions.

Each number consists of three binary digits. Each binary value represents a permission. If the value is 1, the permission is enabled. If the value is 0, the permission is not enabled. The following table lists each number and the corresponding permissions.

##0No permission 1Executable permission2Writable permission3Writable and executable4Readable permissions5Readable and executable6Readable and writable7Readable, writable and executable
Number Permission
Suppose we want to change the "/public_html/new_dir" directory If the owner permissions are set to readable, writable and executable, and the group and other user permissions are set to no permissions, you can use the following code:

$dir_permission = "0700";
if (ftp_chmod($conn, $dir_permission, "/public_html/new_dir")) {
   echo "修改目录权限成功";
} else {
   echo "修改目录权限失败";
}
Copy after login
In the above code, we use "0700 " to set the directory permissions. The first number of the value is the owner permissions, which are set to "7", which means read, write, and executable. The second and third numbers are both "0", indicating that neither the group nor other users have permissions.

    Close the FTP connection
After performing all operations, we should use the "ftp_close" function to close the connection to the FTP server. As shown below:

// 关闭 FTP 连接
ftp_close($conn);
Copy after login
Summary

In this article, we learned how to use FTP functions in PHP code to create a directory and set permissions. First, we establish a connection to the FTP server using the "ftp_connect" and "ftp_login" functions. We then create the directory using the "ftp_mkdir" function and set the directory permissions using the "ftp_chmod" function. Finally, we close the connection to the FTP server using the "ftp_close" function.

The above is the detailed content of How to create a directory on ftp and set permissions in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!