首页 > 作文

PHP的FTP学习(二)[转自奥索]

更新时间:2023-04-07 04:42:04 阅读: 评论:0

现在终于到了我们的第三个文件,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);

——————————————————————————–

以下是代码列表:

———R通用技术12;——————————————————————–

<!– code for index.html begins here –>

<html>

<head>

<bafont face=arial>

</head>

<body>

<table border=0 align=center>

<form action=”actions.php” method=post>

<input type=hidden name=action value=cwd>

<tr>

<td>

rver

</td>

<td>

<input type=text name=rver>

</td>

</tr>

<tr>

<td>

ur

</td>

<td>

<input type=text name=urname>

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

<bafont face=arial>

</head>

<body>

<?

/*

——————————————————————————–

disclaimer:

this is u-at-your-own-risk code.

it is meant only for illustrative purpos and is not meant for production environments. no warranties of any kind are provided to th翻来覆去的意思e ur.

you have been warned!

all code copyright melonfire, 2000. visit us at http://www.melonfire.com

——————————————————————————我学会了坚强211;

*/

// function to connect to ftp rver

function connect()

{

global $rver, $urname, $password;

$conn = ftp_connect($rver);

ftp_login($conn, $urname, $password);

return $conn;

}

// main program begins

// check for valid form entries el print error

if (!$rver

!$urname

!$password)

{

echo “form data incomplete!”;

}

el

{

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

el

{

$path = $cdir . “/” . $rdir;

}

// change to requested directory

ftp_chdir($result, $path);

}

// action: delete file(s)

el if ($action == “delete”)

{

ftp_chdir($result, $cdir);

// loop through lected files and delete

for ($x=0; $x<sizeof($dfile); $x++)

{

ftp_delete($result, $cdir . “/” . $dfile[$x]);

}

}

// action: download files

el if ($action == “download”)

{

ftp_chdir($result, $cdir);

// download lected 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

el if ($action == “upload”)

{

ftp_chdir($result, $cdir);

// put file

/*

a better idea would be to u

$res_code = ftp_put($result, $http_post_files[“upfile”][“name”],

$http_post_files[“upfile”][“tmp_name”], ftp_binary);

as it offers greater curity

*/

$res_code = ftp_put($result, 圣索菲亚大教堂$upfile_name, $upfile, ftp_binary);

// check status and display

if ($res_code == 1)

{

$status = “upload successful!”;

}

el

{

$status = “upload error!”;

}

}

// create file list

$filelist = ftp_nlist($result, “.”);

// and display interface

include(“include.php”);

// clo 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 ction 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++;

}

el

{

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

<!– the values are pasd hidden every time –>

<!– a more optimal solution might be to place the in ssion

variables –>

<input type=hidden name=urname value=<? echo $urname; ?>>

<input type=hidden name=password value=<? echo $password; ?>>

<input type=hidden name=rver value=<? echo $rver; ?>>

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

<lect 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>”;

}

?>

</lect>

<input type=submit value=go>

</form>

<hr>

<!– file listing begins –>

available files:

<form action=actions.php method=post>

<!– the values are pasd hidden every time –>

<input type=hidden name=rver value=<? echo $rver; ?>>

<input type=hidden name=urname value=<? echo $urname; ?>>

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

<!– the values are pasd hidden every time –>

<input type=hidden name=urname value=<? echo $urname; ?>>

<input type=hidden name=password value=<? echo $password; ?>>

<input type=hidden name=rver value=<? echo $rver; ?>>

<input type=hidden name=cdir value=<? echo $here; ?>>

<table>

<tr>

<td>

<!– file lection 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 –>

本文发布于:2023-04-07 04:42:02,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/08743a44720e66baddb5d4bcad6565e4.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP的FTP学习(二)[转自奥索].doc

本文 PDF 下载地址:PHP的FTP学习(二)[转自奥索].pdf

标签:表单   文件   文件名   变量
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图