PHP | ftp_alloc()函数
ftp_alloc()函数是PHP中的一个内置函数,用于为要上传到 FTP 服务器的文件分配空间。
句法:
ftp_alloc( $ftp_connection, $filesize, $result );
参数:此函数接受三个参数,如上所述,如下所述:
- $ftp_connection:必填参数。它指定用于分配空间和上传文件的现有 FTP 连接。
- $filesize:必填参数。它以字节为单位指定文件的大小,即要分配的字节数。
- $result:这个参数是可选的。它指定一个变量来存储响应。
返回值:成功时返回True,失败时返回False。
笔记:
- 此函数适用于PHP 5.0.0 及更新版本。
- 以下示例无法在在线 IDE 上运行。因此,请尝试在一些PHP托管服务器或具有正确 ftp 服务器名称的 localhost 中运行。
下面的例子说明了PHP中的 ftp_alloc()函数:
示例 1:
php
logged in successfully!";
// Allocating space for the file as specified
// i.e. in $file
if(ftp_alloc($ftp_connection, filesize($file), $result)) {
echo "
space successfully allocated on the FTP server";
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 "
space allocation failed!";
}
}
else {
echo "
login failed!";
}
// Closing connection
if(ftp_close($ftp_connection)) {
echo "
Connection closed Successfully!";
}
}
?>
php
logged in successfully!";
// Allocating space for the file as specified
// i.e. in $file
if(ftp_alloc($ftp_connection, filesize($file), $result)) {
echo "
space successfully allocated on the FTP server";
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 "
space allocation failed!";
}
}
else {
echo "
login failed!";
}
// Closing connection
if(ftp_close($ftp_connection)) {
echo "
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
logged in successfully!";
// Allocating space for the file as specified
// i.e. in $file
if(ftp_alloc($ftp_connection, filesize($file), $result)) {
echo "
space successfully allocated on the FTP server";
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 "
space allocation failed!";
}
}
else {
echo "
login failed!";
}
// Closing connection
if(ftp_close($ftp_connection)) {
echo "
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/函数.ftp-alloc。 PHP