PHP | ftp_put()函数
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 服务器的文件的路径。
- $mode:必填参数。它指定传输模式。参数的值是 FTP_ASCII 或 FTP_BINARY。
- $start_position:可选参数。它指定远程文件中开始上传的位置。
返回值:成功时返回True,失败时返回False。
笔记:
- 此函数在PHP 4.0.0 及更新版本上可用。
- 以下示例无法在在线 IDE 上运行。因此,请尝试在一些PHP托管服务器或具有正确 ftp 服务器名称的 localhost 中运行。
下面的例子说明了 ftp_put()函数在PHP中的使用:
示例 1:
php
logged in successfully!";
if (ftp_put($ftp_connection,
"uploadedfile_name_in_server.txt", $file, FTP_ASCII))
{
echo "
Uploaded successful $file.";
}
else {
echo "
Error while uploading $file.";
}
}
else {
echo "
login failed!";
}
// Closing the connection
if(ftp_close($ftp_connection)) {
echo "
Connection closed Successfully!";
}
}
?>
php
logged in successfully!";
if (ftp_put($ftp_connection,
"uploadedfile_name_in_server.txt", $file, FTP_ASCII))
{
echo "
Uploaded successful $file.";
}
else {
echo "
Error while uploading $file.";
}
}
else {
echo "
login failed!";
}
// Echo ftp_get_option($ftp_connection, 1);
// Closing connection
if(ftp_close($ftp_connection)) {
echo "
Connection closed Successfully!";
}
}
?>
输出:
Successfully connected to the ftp server!
logged in successfully!
Uploaded successful filetoupload.txt.
Connection closed Successfully!
示例 2:使用端口号 21 连接到 ftp 服务器,然后上传文件。
PHP
logged in successfully!";
if (ftp_put($ftp_connection,
"uploadedfile_name_in_server.txt", $file, FTP_ASCII))
{
echo "
Uploaded successful $file.";
}
else {
echo "
Error while uploading $file.";
}
}
else {
echo "
login failed!";
}
// Echo ftp_get_option($ftp_connection, 1);
// Closing connection
if(ftp_close($ftp_connection)) {
echo "
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/ 函数.ftp-put。 PHP