Now we finally get to our third file, include. php, which creates a user interface for the program.
"Include. php" contains three forms. Some PHP code obtains the current directory list and stores them in three variables.
$ Files (including files in the current directory ),
$ File_sizes (corresponding file size ),
And $ dirs (including subdirectory name)
The first form generates a drop-down directory list using $ dirs, corresponding to "action = CWD ".
The second form uses $ files $ file_sizes to create an available file list. Each file uses a checkbox. The action of this form corresponds to "action = Delete" and "action = Download"
The third form is used to upload a file to the FTP site, as shown below:
--------------------------------------------------------------------------------
<Form enctype = "multipart/form-data" action = actions. php4 method = post>
...
<Input type = file name = upfile>
...
</Form>
--------------------------------------------------------------------------------
When PHP receives a file name in this way, some variables are generated. These variables specify the file size, a temporary file name, and the file type. The initial file name contains $ upfile_name, once uploaded, the file name is saved to $ upfile (this variable is created by PHP)
With this information, we can create the following statements:
--------------------------------------------------------------------------------
Ftp_put ($ result, $ upfile_name, $ upfile, FTP_BINARY );
--------------------------------------------------------------------------------
The following is a list of codes:
--------------------------------------------------------------------------------
<! -- Code for index.html begins here -->
<Html>
<Head>
<Basefont face = arial>
</Head>
<Body>
<Table border = 0 align = center>
<Form action = "actions. php" method = post>
<Input type = hidden name = action value = CWD>
<Tr>
<Td>
Server
</Td>
<Td>
<Input type = text name = server>
</Td>
</Tr>
<Tr>
<Td>
User
</Td>
<Td>
<Input type = text name = username>
</Td>
</Tr>
<Tr>
<Td>
Password
</Td>
<Td>
<Input type = password name = password>
</Td>
</Tr>
<Tr>
<Td colspan = 2 align = center>
<Input type = "submit" value = "Beam Me Up, Scotty! ">
</Td>
</Tr>
</Form>
</Table>
</Body>
</Html>
<! -- Code for index.html ends here -->
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
<! -- Code for actions. php begins here -->
<Html>
<Head>
<Basefont face = Arial>
</Head>
<Body>
<?
/*
--------------------------------------------------------------------------------
DISCLAIMER:
This is use-at-your-own-risk code.
It is meant only for your strative purposes and is not meant for production environments. No warranties of any kind are provided to the user.
You have been warned!
All code copyright Melonfire, 2000. Visit us at http://www.melonfire.com
--------------------------------------------------------------------------------
*/
// Function to connect to FTP server
Function connect ()
{
Global $ server, $ username, $ password;
$ Conn = ftp_connect ($ server );
Ftp_login ($ conn, $ username, $ password );
Return $ conn;
}
// Main program begins
// Check for valid form entries else print error
If (! $ Server
! $ Username
! $ Password)
{
Echo "Form data incomplete! ";
}
Else
{
// Connect
$ Result = connect ();
// Action: change directory
If ($ action = "CWD ")
{
// At initial stage $ rdir does not exist
// So assume default directory
If (! $ Rdir)
{
$ Path = ".";
}
// Get current location $ cdir and add it to requested directory $ rdir
Else
{
$ Path = $ cdir. "/". $ rdir;
}
// Change to requested directory
Ftp_chdir ($ result, $ path );
}
// Action: delete file (s)
Else if ($ action = "Delete ")
{
Ftp_chdir ($ result, $ cdir );
// Loop through selected files and delete
For ($ x = 0; $ x <sizeof ($ dfile); $ x ++)
{
Ftp_delete ($ result, $ cdir. "/". $ dfile [$ x]);
}
}
// Action: download files
Else if ($ action = "Download ")
{
Ftp_chdir ($ result, $ cdir );
// Download selected files
// IMPORTANT: you shoshould specify a different download location here !!
For ($ x = 0; $ x <sizeof ($ dfile); $ x ++)
{
Ftp_get ($ result, $ dfile [$ x], $ dfile [$ x], FTP_BINARY );
}
}
// Action: upload file
Else if ($ action = "Upload ")
{
Ftp_chdir ($ result, $ cdir );
// Put file
/*
A better idea wocould be to use
$ Res_code = ftp_put ($ result, $ HTTP_POST_FILES ["upfile"] ["name"],
$ HTTP_POST_FILES ["upfile"] ["tmp_name"], FTP_BINARY );
As it offers greater security
*/
$ Res_code = ftp_put ($ result, $ upfile_name, $ upfile, FTP_BINARY );
// Check status and display
If ($ res_code = 1)
{
$ Status = "Upload successful! ";
}
Else
{
$ Status = "Upload error! ";
}
}
// Create file list
$ Filelist = ftp_nlist ($ result ,".");
// And display interface
Include ("include. php ");
// Close connection
Ftp_quit ($ result );
}
?>
</Body>
</Html>
<! -- Code for actions. php ends here -->
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
<! -- Code for include. php begins here -->
<?
// Get current location
$ Here = ftp_pwd ($ result );
/*
Since ftp_size () is quite slow, especially when working
On an array containing all the files in a directory,
This section performs an ftp_size () on all the files in the current
Directory and creates three arrays.
*/
// Array for files
$ Files = Array ();
// Array for directories
$ Dirs = Array ();
// Array for file sizes
$ File_sizes = Array ();
// Counters
$ File_list_counter = 0;
$ Dir_list_counter = 0;
// Check each element of $ filelist
For ($ x = 0; $ x <sizeof ($ filelist); $ x ++)
{
If (ftp_size ($ result, $ filelist [$ x])! =-1)
{
// Create arrays
$ Files [$ file_list_counter] = $ filelist [$ x];
$ File_sizes [$ file_list_counter] = ftp_size ($ result, $ filelist [$ x]);
$ File_list_counter ++;
}
Else
{
$ Dir_list [$ dir_list_counter] = $ filelist [$ x];
$ Dir_list_counter ++;
}
}
?>
<! -- Header-where am I? -->
<Center>
You are currently working in <B> <? Echo $ here;?> </B>
<Br>
<! -- Status message for upload function -->
<? Echo $ status;?>
</Center>
<Hr>
<P>
<! -- Directory listing in drop-down list -->
Available directories:
<Form action = actions. php method = post>
<! -- These values are passed hidden every time -->
<! -- A more optimal solution might be to place these in session
Variables -->
<Input type = hidden name = username value = <? Echo $ username;?>
<Input type = hidden name = password value = <? Echo $ password;?>
<Input type = hidden name = server value = <? Echo $ server;?>
<Input type = hidden name = cdir value = <? Echo $ here ;?>>
<! -- Action to take when THIS form is submitted -->
<Input type = hidden name = action value = CWD>
<! -- Dir listing begins; first item is for parent dir -->
<Select name = rdir>
<Option value = "..."> <parent directory> </option>
<?
For ($ x = 0; $ x <sizeof ($ dir_list); $ x ++)
{
Echo "<option value =". $ dir_list [$ x]. ">". $ dir_list [$ x]. "</option> ";
}
?>
</Select>
<Input type = submit value = Go>
</Form>
<Hr>
<! -- File listing begins -->
Available files:
<Form action = actions. php method = post>
<! -- These values are passed hidden every time -->
<Input type = hidden name = server value = <? Echo $ server;?>
<Input type = hidden name = username value = <? Echo $ username;?>
<Input type = hidden name = password value = <? Echo $ password;?>
<Input type = hidden name = cdir value = <? Echo $ here ;?>>
<Table border = 0 width = 100%>
<?
// Display file listing with checkboxes and sizes
For ($ y = 0; $ y <sizeof ($ files); $ y ++)
{
Echo "<tr> <td> <input type = checkbox name = dfile [] value =". $ files [$ y].
">". $ Files [$ y]. "<I> (". $ file_sizes [$ y]. "bytes) </I> <td> ";
}
?>
</Table>
<! -- Actions for this form -->
<Center>
<Input type = submit name = action value = Delete>
<Input type = submit name = action value = Download>
</Center>
</Form>
<P>
<Hr>
<! -- File upload form -->
File upload:
<Form enctype = "multipart/form-data" action = actions. php method = post>
<! -- These values are passed hidden every time -->
<Input type = hidden name = username value = <? Echo $ username;?>
<Input type = hidden name = password value = <? Echo $ password;?>
<Input type = hidden name = server value = <? Echo $ server;?>
<Input type = hidden name = cdir value = <? Echo $ here ;?>>
<Table>
<Tr>
<Td>
<! -- File selection box -->
<Input type = file name = upfile>
</Td>
</Tr>
<Tr>
<Td>
<! -- Action for this form -->
<Input type = submit name = action value = Upload>
</Td>
</Tr>
</Table>
</Form>
<! -- Code for include. php ends here -->