时间:2021-05-26
现在终于到了我们的第三个文件,include.php 它为程序建立起一个用户界面。
"include.php" 包含三个表单,一些PHP代码获取当前的目录列表并将它们存入三个变量
$files (包括当前目录下的文件),
$file_sizes (相应的文件大小),
and $dirs (包含子目录名)
第一个表单使用$dirs 产生一个下拉式目录列表,对应于“action=CWD”。
第二个表单使用$files$file_sizes创建一个可用的文件列表,每一个文件使用一个checkbox。这个表单的action对应于"action=Delete" and "action=Download"
第三个表单用来上传一个文件到FTP站点,如下:
--------------------------------------------------------------------------------
<form enctype="multipart/form-data" action=actions.php4 method=post>
...
<input type=file name=upfile>
...
</form>
--------------------------------------------------------------------------------
当PHP以这种方式接收到一个文件名,一些变量就产生了,这些变量指定文件的大小,一个临时的文件名以及文件的类型,最初的文件名存在$upfile_name,一旦上传后文件名便存入$upfile中(这个变量是由PHP自己创建的)
通过这些信息,我们就可以创建以下的语句了:
--------------------------------------------------------------------------------
ftp_put($result, $upfile_name, $upfile, FTP_BINARY);
--------------------------------------------------------------------------------
以下是代码列表:
--------------------------------------------------------------------------------
<!-- 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 illustrative 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://plete!";
}
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 should 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
$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);
// 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 -->
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
来源:奥索网桌面中心(二)数据库写入作者:elong一、以下是数据库的写入的html程序,你可以加入密码功能。把密码做成变量发入下面那个写入的php程序。这样就
reply.php(用来回复主题)疑难问题【本文版权归作者与奥索网共同拥有,如需转载,请注明作者及出处】
来源:奥索网桌面中心(四)数据显示作者:elong最后一步就是显示了。下面这个程序要通过参数来调用。比如php.php?jpg=jpg后面的jpg是表格名。记住
FTP上传是PHP实现的一个常见且非常重要的应用技巧,今天就来与大家分享一下PHP实现FTP上传文件的简单示例。希望对大家的PHP学习能带来一定的帮助。主要代码
【本文版权归作者与奥索网共同拥有,如需转载,请注明作者及出处】