Php ftp Learning (4)

Source: Internet
Author: User

By Vikram Vaswani
Melonfire
November 07,200 0
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 -->

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.