Using PHP to write FTP file Upload function Tutorial _php tutorial

Source: Internet
Author: User
Tags ftp file ftp login ftp client
This tutorial is about using PHP to write FTP file upload function tutorial Oh, PHP function is very powerful, you can use his own function to instance the FTP function, to achieve file upload function oh

This tutorial is about using PHP tutorial to write FTP file upload function Tutorial Oh, PHP is very powerful, you can use his own function to instance the FTP function, to achieve file upload function Oh. The following is a complete example

function Phpftp_top () {
echo " n";
echo "n";
echo "n";
echo "PHP FTP Client v1.4n ";
echo "n";
echo "n";
echo "NN";
}

function Phpftp_bottom () {
echo "n n";
echo "n";
echo "n";
echo "n";
}

function Phpftp_login () {
Phpftp_top ();
echo " n";
echo "

n ";
echo "n";
echo "

n ";
Phpftp_bottom ();
}

/* This function does not return true/false-it returns the value of
$FTP, the current FTP stream. */

function Phpftp_connect ($phpftp _user, $phpftp _passwd) {
$phpftp _host= "localhost";
$ftp = Ftp_connect ($phpftp _host);
if ($ftp) {
if (Ftp_login ($ftp, $phpftp _user, $phpftp _passwd)) {
return $ftp;
}
}
}

function Phpftp_list ($phpftp _user, $phpftp _passwd, $phpftp _dir) {
Phpftp_top ();
echo " n";
$ftp = @phpftp_connect ($phpftp _user, $phpftp _passwd);
if (! $ftp) {
echo "FTP login failed! n ";
echo "Start over?n";
Phpftp_bottom ();
} else {
if (! $phpftp _dir) {
$phpftp _dir=ftp_pwd ($FTP);
}
if (! @ftp_chdir ($ftp, $phpftp _dir)) {
echo "Can ' t enter that directory!

n ";
$phpftp _dir=ftp_pwd ($FTP);
}
echo "Currentdirectory: ". $phpftp _dir. "
n ";
if ($phpftp _dir = = = "/") {
$phpftp _dir= "";
}
echo "



















Directory empty or not readable

n ";
}
echo "

n "; if ($contents = Ftp_nlist ($ftp, ".")) {$d _i=0; $f _i=0; sort ($contents); for ($i =0; $i < count ($contents); $i + +) {$file _size = ftp_size ($ftp, $contents [$i]); if ($file _size = =-1) {$nlist _dirs[$d _i]= $contents [$i]; $d _i++;} else {$nlist _files[$f _i]= $contents [$i]; $nlist _files ize[$f _i]= $file _size; $f _i++; }} echo " nn "; } else {echo '
n ";
echo "directories
n ";
echo "n";
echo "
nn ";
echo "Files
n ";
echo "n";
echo "
nn ";
echo "

n ";
echo "n";
echo "

n ";
echo "n";
echo "

nn ";
echo "

n ";
echo " n ";
echo " n ";
echo " n ";
echo " n ";
echo " n ";
echo " n ";
Ftp_quit ($FTP);
Phpftp_bottom ();
}
}

function Phpftp_cd ($phpftp _user, $phpftp _passwd, $phpftp _dir, $select _directory) {
echo " n";
$new _directory= $phpftp _dir. "/" . $select _directory;
Phpftp_list ($phpftp _user, $phpftp _passwd, $new _directory);
}

function Phpftp_mkdir ($phpftp _user, $phpftp _passwd, $phpftp _dir, $new _dir) {
echo " n";
$ftp = @phpftp_connect ($phpftp _user, $phpftp _passwd);
if ($phpftp _dir = = "") {
$phpftp _dir= "/";
}
if (! $ftp) {
@ftp_quit ($FTP);
Phpftp_top ();
echo "FTP login failed!

n ";
echo "Start over?n";
Phpftp_bottom ();
} else {
$dir _path = $phpftp _dir. "/" . $new _dir;
@ftp_mkdir ($ftp, $dir _path);
@ftp_quit ($FTP);
Phpftp_list ($phpftp _user, $phpftp _passwd, $phpftp _dir);
}
};


function Phpftp_get ($phpftp _user, $phpftp _passwd, $phpftp _dir, $select _file) {
$ftp = @phpftp_connect ($phpftp _user, $phpftp _passwd);
if ($phpftp _dir = = "") {
$phpftp _dir= "/";
}
if ((! $ftp) | | (! @ftp_chdir ($ftp, $phpftp _dir))) {
@ftp_quit ($FTP);
Phpftp_top ();
echo "FTP login failed!

n ";
echo "Start over?n";
Phpftp_bottom ();
} else {
Srand (Double) microtime () *1000000);
$randval = rand ();
$tmpfile = "/var/tmp/xfers/". $select _file. "." . $randval;
if (! Ftp_get ($ftp, $tmpfile, $select _file, ftp_binary)) {
Ftp_quit ($FTP);
Phpftp_top ();
echo "FTP get failed!

Nstart Over?n ";
Phpftp_bottom ();
} else {
Ftp_quit ($FTP);
Header ("Content-type:application/octet-stream");
Header ("content-disposition:attachment; Filename= ". $select _file);
ReadFile ($tmpfile);
}
@unlink ($tmpfile);
}
}

function Phpftp_put ($phpftp _user, $phpftp _passwd, $phpftp _dir, $userfile, $userfile _name) {
Srand (Double) microtime () *1000000);
$randval = rand ();
$tmpfile = "/var/tmp/xfers/". $userfile _name. "." . $randval;
if (! @copy ($userfile, $tmpfile)) {
Phpftp_top ();
echo "Upload failed! Probably no space on server for temp file.

Nstart Over?n ";
Phpftp_bottom ();
} else {
if (! $ftp = @phpftp_connect ($phpftp _user, $phpftp _passwd)) {
Unlink ($tmpfile);
Phpftp_top ();
echo "FTP login failed!

Nstart Over?n ";
Phpftp_bottom ();
} else {
Ftp_put ($ftp, $userfile _name, $tmpfile, ftp_binary);
Ftp_quit ($FTP);
Unlink ($tmpfile);
Phpftp_list ($phpftp _user, $phpftp _passwd, $phpftp _dir);
}
}
}

Switch ($function) {
Case "DIR";
Phpftp_list ($phpftp _user, $phpftp _passwd, $phpftp _dir);
Break
Case "CD";
PHPFTP_CD ($phpftp _user, $phpftp _passwd, $phpftp _dir, $select _directory);
Break
Case "GET";
Phpftp_get ($phpftp _user, $phpftp _passwd, $phpftp _dir, $select _file);
Break
Case "put";
Phpftp_put ($phpftp _user, $phpftp _passwd, $phpftp _dir, $userfile, $userfile _name);
Break
Case "mkdir";
Phpftp_mkdir ($phpftp _user, $phpftp _passwd, $phpftp _dir, $new _dir);
Break
Case "";
Phpftp_login ();
Break
}

?>


http://www.bkjia.com/PHPjc/444933.html www.bkjia.com true http://www.bkjia.com/PHPjc/444933.html techarticle This tutorial is about using PHP to write FTP file upload function tutorial Oh, PHP function is very powerful, you can use his own function to instance the FTP function, to achieve file upload function Oh this tutorial ...

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