PHP如何使用ftp_put()函数?代码示例

2021年3月27日14:21:23 发表评论 1,023 次浏览

ftp_put()函数是PHP中的内置函数, 用于将文件上传到FTP服务器。

语法如下:

ftp_put( $ftp_connection, $remote_file_path, $local_file_path, $mode, $start_position );

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

  • $ ftp_connection:它是必填参数。它指定用于上传文件的现有FTP连接。
  • $ remote_file_path:它是必填参数。它指定了远程服务器(即FTP服务器)中要上传文件的路径。
  • $ local_file_path:它是必填参数。它指定要在FTP服务器中上载文件的路径。
  • $模式:它是必填参数。它指定传输模式。该参数的值为FTP_ASCII或FTP_BINARY。
  • $ start_position:它是可选参数。它指定了远程文件中开始上传到的位置。

返回值:成功返回True, 失败返回False。

注意:

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

以下示例说明了PHP中ftp_put()函数的用法:

范例1:

<?php
   
// Connect to FTP server
$ftp_server = "localhost" ;
   
// Use correct ftp username
$ftp_username = "user" ;
   
// Use correct ftp password corresponding
// to the ftp username
$ftp_userpass = "user" ;
   
// File name or path to upload to ftp server
$file = "filetoupload.txt" ;
    
// Establishing ftp connection 
$ftp_connection = ftp_connect( $ftp_server ) 
     or die ( "Could not connect to $ftp_server" );
   
if ( $ftp_connection ) {
     echo "Successfully connected to the ftp server!" ;
       
     // Logging in to established connection with
     // ftp username password
     $login = ftp_login( $ftp_connection , $ftp_username , $ftp_userpass );
       
     // Checking whether logged in successfully or not
     if ( $login ) {
         echo "<br>logged in successfully!" ;
           
         if (ftp_put( $ftp_connection , "uploadedfile_name_in_server.txt" , $file , FTP_ASCII))
         {
           echo "<br>Uploaded successful $file." ;
         }
         else {
           echo "<br>Error while uploading $file." ;
         }
            
     }
     else {
         echo "<br>login failed!" ;
     }
   
     // Closeing the  connection
     if (ftp_close( $ftp_connection )) {
         echo "<br>Connection closed Successfully!" ;
     } 
}
?>

输出如下:

Successfully connected to the ftp server!
logged in successfully!
Uploaded successful filetoupload.txt.
Connection closed Successfully!

范例2:使用端口号21连接到ftp服务器, 然后上传文件。

<?php
  
// Connect to FTP server
  
// Server name
$ftp_server = "localhost" ;
  
// Use correct ftp username
$ftp_username = "user" ;
  
// Use correct ftp password corresponding
// to the ftp username
$ftp_userpass = "user" ;
  
// File name or path to upload to ftp server
$file = "filetoupload.txt" ;
   
// Establishing ftp connection 
$ftp_connection = ftp_connect( $ftp_server , 21) 
     or die ( "Could not connect to $ftp_server" );
  
if ( $ftp_connection ) {
     echo "Successfully connected to the ftp server!" ;
      
     // Logging in to established connection with
     // ftp username password
     $login = ftp_login( $ftp_connection , $ftp_username , $ftp_userpass );
     if ( $login ) {
          
         // Checking whether logged in successfully or not
         echo "<br>logged in successfully!" ;
          
         if (ftp_put( $ftp_connection , "uploadedfile_name_in_server.txt" , $file , FTP_ASCII)) 
         {
           echo "<br>Uploaded successful $file." ;
         }
         else {
           echo "<br>Error while uploading $file." ;
         }
     }
     else {
         echo "<br>login failed!" ;
     }
      
     // Echo ftp_get_option($ftp_connection, 1);
     // Closeing  connection
     if (ftp_close( $ftp_connection )) {
         echo "<br>Connection closed Successfully!" ;
     } 
}
  
?>

输出如下:

Successfully connected to the ftp server!
logged in successfully!
Uploaded successful filetoupload.txt.
Connection closed Successfully!

参考: https://www.php.net/manual/en/function.ftp-put.php


木子山

发表评论

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