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:它是可选参数。它指定了远程文件中开始上传到的位置。
【PHP如何使用ftp_put()函数(代码示例)】注意:
- 此功能在PHP 4.0.0和更高版本上可用。
- 以下示例无法在在线IDE上运行。因此, 请尝试使用适当的ftp服务器名称在某些PHP托管服务器或localhost中运行。
范例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 theconnection
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);
// Closeingconnection
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
推荐阅读
- PHP如何使用gmp_div_q()函数(代码示例)
- Java如何实现多线程聊天应用程序(S2(客户端编程))
- Python中的构造函数用法快速指南
- Scala的特征线性化详细指南
- Materialize CSS颜色介绍和用法示例
- XP设置系统优化精华 让系统跑得更快
- 优化xp内存7步走 体验奔驰般的速度
- 详细解说WinXP的还原技巧的运用
- Windows系统防病毒软件的共性