ftp_alloc()函数是PHP中的内置函数, 用于为要在FTP服务器中上传的文件分配空间。
语法如下:
ftp_alloc( $ftp_connection, $filesize, $result );
参数:此函数接受上述和以下描述的三个参数:
- $ ftp_connection:它是必填参数。它指定用于分配空间和上载文件的现有FTP连接。
- $ filesize:它是必填参数。它以字节为单位指定文件的大小, 即要分配的字节数。
- $result:此参数是可选的。它指定一个变量来存储响应。
返回值:成功返回True, 失败返回False。
注意:
- 此功能可用于PHP 5.0.0和更高版本。
- 以下示例无法在在线IDE上运行。因此, 请尝试使用适当的ftp服务器名称在某些PHP托管服务器或localhost中运行。
以下示例说明了PHP中的ftp_alloc()函数:
范例1:
<?php
// Connect to FTP server
// Use a correct 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 );
if ( $login ) {
// Checking whether logged in successfully or not
echo "<br>logged in successfully!" ;
// Allocating space for the file as specified
// i.e. in $file
if (ftp_alloc( $ftp_connection , filesize ( $file ), $result )) {
echo "<br>space successfully allocated on the FTP server" ;
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>space allocation failed!" ;
}
}
else {
echo "<br>login failed!" ;
}
// Closeing connection
if (ftp_close( $ftp_connection )) {
echo "<br>Connection closed Successfully!" ;
}
}
?>
输出如下:
successfully connected to the ftp server!
logged in successfully!
space successfully allocated on the FTP server
Uploaded successful filetoupload.txt.
Connection closed Successfully!
范例2:使用端口号21连接到ftp服务器, 然后分配和上传文件。
<?php
// Connect to FTP server
// Use a correct 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 , 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!" ;
// Allocating space for the file as specified
// i.e. in $file
if (ftp_alloc( $ftp_connection , filesize ( $file ), $result )) {
echo "<br>space successfully allocated on the FTP server" ;
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>space allocation failed!" ;
}
}
else {
echo "<br>login failed!" ;
}
// Closeing connection
if (ftp_close( $ftp_connection )) {
echo "<br>Connection closed Successfully!" ;
}
}
?>
输出如下:
successfully connected to the ftp server!
logged in successfully!
space successfully allocated on the FTP server
Uploaded successful filetoupload.txt.
Connection closed Successfully!
参考: https://www.php.net/manual/en/function.ftp-alloc.php