By Vikram Vaswani
Melonfire
November 07, 2000
Log in to the FTP server, PHP provides some functions that can obtain some information about the system, files and directories.
ftp_pwd()
If you want to know the directory you are currently in, you need to use this function.
--------------------------------------------------------------------------------
<?
// get current location
$here = ftp_pwd($conn);
?>
--------------------------------------------------------------------------------
What if you need to know what system is running on the server side?
ftp_systeme() just provides you with information about this.
--------------------------------------------------------------------------------
<?
// get system type
$server_os = ftp_systype($conn);
?>
--------------------------------------------------------------------------------
Regarding the switch of passive mode (PASV), PHP also provides a function that can turn PASV on or off (1 means on)
--------------------------------------------------------------------------------
<?
// turn PASV on
ftp_pasv($conn, 1);
?>
--------------------------------------------------------------------------------
Now that you already know where you are with you, we're going to stroll through the directory -- the ftp_chdir() function that implements this function, which accepts a directory name as a parameter.
--------------------------------------------------------------------------------
<?
// change directory to "public_html"
ftp_chdir($conn, "public_html");
?>
--------------------------------------------------------------------------------
If you want to go back to the directory you were in (parent directory), ftp_cdup() can help you realize your wish, and this function can go back to the previous level directory.
--------------------------------------------------------------------------------
<?
// go up one level in the directory tree
ftp_cdup($conn);
?>
--------------------------------------------------------------------------------
You can also create or move a directory, which requires the use of the ftp_mkdir() and ftp_rmdir() functions; note: if ftp_mkdir() is successfully established, the newly created directory name will be returned.
--------------------------------------------------------------------------------
<?
// make the directory "test"
ftp_mkdir($conn, "test");
// remove the directory "test"
ftp_rmdir($conn, "test");
?>
--------------------------------------------------------------------------------
Creating an FTP directory is usually to transfer files--- So let's get started!
First, upload the file. The ftp_put() function is very competent for this responsibility. It requires you to specify a local file name, uploaded file name and transfer type. For example: If you want to upload the file "" and then name it "" after uploading, the command should be like this:
--------------------------------------------------------------------------------
<?
// upload
ftp_put($conn, "", "", FTP_ASCII);
?>
--------------------------------------------------------------------------------
Download the file:
The function provided by PHP is ftp_get(), which also requires a file name on the server, the downloaded file name, and the transmission type as parameters. For example: the server side file is, you want to download it to the local machine and name it, the command is as follows:
--------------------------------------------------------------------------------
<?
// download
ftp_get($conn, "", "", FTP_BINARY);
?>
--------------------------------------------------------------------------------
PHP defines two modes as transmission modes FTP_BINARY and FTP_ASCII. Please read the above two examples of the use of these two modes. As for their detailed explanation, I won’t say much about this article. Please refer to the relevant books for details.
How should I list the files? (Use DIR? :) )
PHP provides two methods: one is to simply list the file name and directory, and the other is to list the file size, permissions, creation time and other information in detail.
The first one uses the ftp_nlist() function, and the second one uses ftp_rawlist(). Both functions require a directory name as a parameter, and both return the directory column as an array. Each element of the array is equivalent to a row of the list.
--------------------------------------------------------------------------------
<?
// obtain file listing
$filelist = ftp_nlist($conn, ".");
?>
--------------------------------------------------------------------------------
You must want to know the file size! Don't worry, here is a very easy function ftp_size(), which returns the size of the file you specified, using BITES as the unit. It should be noted that if it returns "-1", it means that this is a directory, and in the following examples you will see the application of this feature.
--------------------------------------------------------------------------------
<?
// obtain file size of file ""
$filelist = ftp_size($conn, "");
?>
Melonfire
November 07, 2000
Log in to the FTP server, PHP provides some functions that can obtain some information about the system, files and directories.
ftp_pwd()
If you want to know the directory you are currently in, you need to use this function.
--------------------------------------------------------------------------------
<?
// get current location
$here = ftp_pwd($conn);
?>
--------------------------------------------------------------------------------
What if you need to know what system is running on the server side?
ftp_systeme() just provides you with information about this.
--------------------------------------------------------------------------------
<?
// get system type
$server_os = ftp_systype($conn);
?>
--------------------------------------------------------------------------------
Regarding the switch of passive mode (PASV), PHP also provides a function that can turn PASV on or off (1 means on)
--------------------------------------------------------------------------------
<?
// turn PASV on
ftp_pasv($conn, 1);
?>
--------------------------------------------------------------------------------
Now that you already know where you are with you, we're going to stroll through the directory -- the ftp_chdir() function that implements this function, which accepts a directory name as a parameter.
--------------------------------------------------------------------------------
<?
// change directory to "public_html"
ftp_chdir($conn, "public_html");
?>
--------------------------------------------------------------------------------
If you want to go back to the directory you were in (parent directory), ftp_cdup() can help you realize your wish, and this function can go back to the previous level directory.
--------------------------------------------------------------------------------
<?
// go up one level in the directory tree
ftp_cdup($conn);
?>
--------------------------------------------------------------------------------
You can also create or move a directory, which requires the use of the ftp_mkdir() and ftp_rmdir() functions; note: if ftp_mkdir() is successfully established, the newly created directory name will be returned.
--------------------------------------------------------------------------------
<?
// make the directory "test"
ftp_mkdir($conn, "test");
// remove the directory "test"
ftp_rmdir($conn, "test");
?>
--------------------------------------------------------------------------------
Creating an FTP directory is usually to transfer files--- So let's get started!
First, upload the file. The ftp_put() function is very competent for this responsibility. It requires you to specify a local file name, uploaded file name and transfer type. For example: If you want to upload the file "" and then name it "" after uploading, the command should be like this:
--------------------------------------------------------------------------------
<?
// upload
ftp_put($conn, "", "", FTP_ASCII);
?>
--------------------------------------------------------------------------------
Download the file:
The function provided by PHP is ftp_get(), which also requires a file name on the server, the downloaded file name, and the transmission type as parameters. For example: the server side file is, you want to download it to the local machine and name it, the command is as follows:
--------------------------------------------------------------------------------
<?
// download
ftp_get($conn, "", "", FTP_BINARY);
?>
--------------------------------------------------------------------------------
PHP defines two modes as transmission modes FTP_BINARY and FTP_ASCII. Please read the above two examples of the use of these two modes. As for their detailed explanation, I won’t say much about this article. Please refer to the relevant books for details.
How should I list the files? (Use DIR? :) )
PHP provides two methods: one is to simply list the file name and directory, and the other is to list the file size, permissions, creation time and other information in detail.
The first one uses the ftp_nlist() function, and the second one uses ftp_rawlist(). Both functions require a directory name as a parameter, and both return the directory column as an array. Each element of the array is equivalent to a row of the list.
--------------------------------------------------------------------------------
<?
// obtain file listing
$filelist = ftp_nlist($conn, ".");
?>
--------------------------------------------------------------------------------
You must want to know the file size! Don't worry, here is a very easy function ftp_size(), which returns the size of the file you specified, using BITES as the unit. It should be noted that if it returns "-1", it means that this is a directory, and in the following examples you will see the application of this feature.
--------------------------------------------------------------------------------
<?
// obtain file size of file ""
$filelist = ftp_size($conn, "");
?>