PHP's FTP learning (d) _php Basics

Source: Internet
Author: User
Tags parent directory
by Vikram Vaswani
Melonfire
November 07, 2000
The following is a list of codes:
--------------------------------------------------------------------------------
<!--code for index.html begins-->
<basefont face=arial>

<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>
&LT;TD colspan=2 align=center>
<input type= "Submit" value= "Beam Me up, scotty!" >
</td>
</tr>

</form>
</table>

</body>

<!--code for index.html ends-->
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
<!--code for actions.php begins-->

<basefont face=arial>
<body>

?
/*
--------------------------------------------------------------------------------
Disclaimer:

This is Use-at-your-own-risk code.

It is meant a for illustrative purposes and isn't 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-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 should specify a different download location
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 would being 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>

<!--code for actions.php ends-->
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
<!--code for include.php begins-->

?

Get Current Location
$here = Ftp_pwd ($result);

/*
Since Ftp_size () is quite slow, especially when working
On ' an ' array containing all of the files in a directory,
This section performs a 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 &LT;B&GT;&LT;? echo $here;?></b>
<br>
<!--status message for upload function-->
? echo $status;?>
</center>
<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
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; The A 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>


<!--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>


<!--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-->

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.