PHP ftp_mkdir()函数用法详细介绍

2021年3月17日18:43:14 发表评论 637 次浏览

ftp_mkdir()函数是PHP中的内置函数, 用于在ftp服务器上创建新目录。创建目录后, 将无法再次创建。创建一个已经存在的目录将产生错误。

语法如下:

string ftp_mkdir( $ftp_connection, $directory_name )

参数:此函数接受上述和以下所述的两个参数:

  • $ ftp_connection:它是必需的参数, 用于指定要在其上创建目录的ftp连接。
  • $ directory_name:它是必需的参数, 用于指定要创建的目录的名称。

如果要在现有或不存在的目录中创建子目录, 则将$ directory_name参数设置为"(父目录名)/(子目录名)/(子目录名的子项)/…"的格式, 因此上。例如, 在testdirectory中创建一个名为childdirectory的目录, 然后$ directory_name =" testdirectory / childdirectory";

返回值:它返回成功创建的目录的名称, 失败返回False。

注意:

  • 此功能可用于PHP 4.0.0和更高版本。
  • 以下示例无法在在线IDE上运行。因此, 请尝试使用适当的ftp服务器名称以及正确的用户名和密码在某些PHP托管服务器或localhost中运行。

范例1:

<?php
// Connecting to ftp server
  
// Use ftp server address
$fserver = "ftp.gfg.org" ;
  
// Use ftp username
$fuser = "username" ;
  
// Use ftp password
$fpass = "password" ;
  
// Connect to the ftp server
$f_conn = ftp_connect( $fserver ) or 
     die ( "Could not connect to $fserver" );
      
// Authenticating to ftp server     
$login = ftp_login( $f_conn , $fuser , $fpass );
  
// Directory name which is to be created
$dir = "testdirectory" ;
  
// Creating directory
if (ftp_mkdir( $f_conn , $dir )) {
      
     // Execute if directory created successfully
     echo " $dir Successfully created" ;
}
else {
      
     // Execute if fails to create directory
     echo "Error while creating $dir" ; 
}
  
// Closeing ftp connection
ftp_close( $f_conn );
  
?>

输出如下:

testdirectory Successfully created
PHP | ftp_mkdir()函数1

范例2:如果要创建子目录, 则除了$ dir即目录名以外, 其他所有内容均与之前相同。

<?php
//Connecting to ftp server
  
// Use ftp server address
$fserver = "ftp.exampleserver.com" ;
  
// Use ftp username
$fuser = "username" ;
  
// Use ftp password
$fpass = "password" ;
  
// Connecting to ftp server
$f_conn = ftp_connect( $fserver ) or
         die ( "Could not connect to $fserver" );
          
// Authenticating to ftp server         
$login = ftp_login( $f_conn , $fuser , $fpass );
  
// Directory name which is to be created
$dir = "testdirectory/childdirectory" ;
  
// Creating directory
if (ftp_mkdir( $f_conn , $dir )) {
      
     // Execute if directory created successfully
     echo " $dir Successfully created" ;
}
else {
      
     // Execute if fails to create directory
     echo "Error while creating $dir" ; 
}
  
// Closeing ftp connection
ftp_close( $f_conn );
  
?>

输出如下:

testdirectory/childdirectory Successfully created
PHP | ftp_mkdir()函数2

注意:如果目录名称已经存在, 则会产生错误。

参考: http://php.net/manual/en/function.ftp-mkdir.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: