Home > Backend Development > PHP Tutorial > PHP FTP Learning (2) [Reprinted from Oso]_PHP Tutorial

PHP FTP Learning (2) [Reprinted from Oso]_PHP Tutorial

WBOY
Release: 2016-07-21 16:11:27
Original
761 people have browsed it


Now we finally get to our third file, include.php which sets up a user interface for the program.

"include.php" contains three forms, some PHP code to get the current directory list and store them into three variables
$files (including files in the current directory),
$ file_sizes (corresponding file sizes),
and $dirs (containing subdirectory names)

The first form uses $dirs to generate a drop-down directory list corresponding to "action=CWD".

The second form uses $files $file_sizes to create a list of available files, and 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 follows:
--------- -------------------------------------------------- ---------------------


.. .

...

------------------ -------------------------------------------------- -----------
When PHP receives a filename in this way, a number of variables are generated that specify the size of the file, a temporary filename, and the file type. Initially The file name exists in $upfile_name. Once uploaded, the file name is stored in $upfile (this variable is created by PHP itself)
With this information, we can create the following statement:
--- -------------------------------------------------- --------------------------
ftp_put($result, $upfile_name, $upfile, FTP_BINARY);
--- -------------------------------------------------- --------------------------
The following is the code list:
--------------------- -------------------------------------------------- ------------------








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

























----- -------------------------------------------------- --------------------------
---------------------- -------------------------------------------------- --------








/*
------------- -------------------------------------------------- ----------------
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 ://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{
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{
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 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);

}
?>





--------------------------------------------------------------------------------
--------------------------------------------------------------------------------



// 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{
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++;
}
}

?>



You are currently working in









Available directories:




>
>
>
>














Available files:



>
>
>
>


Server
< ;/td>



User



Password







// display file listing with checkboxes and sizes
for ($y=0; $y{
echo "
". $files[$y] . " (" . $file_sizes[$y] . " bytes)";
}

?>














File upload:



>
>
>
>

















www.bkjia.comtruehttp://www.bkjia.com/PHPjc/313932.htmlTechArticle现在终于到了我们的第三个文件,include.php 它为程序建立起一个用户界面。 "include.php" 包含三个表单,一些PHP代码获取当前的目录列表并将...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template