PHP ftp class Upload file video, such as the simple implementation of _php example

Source: Internet
Author: User

file 1:ftp.php

<?php/** * Imitation write CodeIgniter FTP class * FTP basic operation: * 1) landing; Connect * 2 List of current directory files; FileList * 3) directory changes; Chgdir * 4) renaming/moving; Rename * 5 Create folder; mkdir * 6) deleted; Delete_dir/delete_file * 7) upload;
Upload * 8 download Download * * * @author Quanshuidingdang/class Ftp {private $hostname = '; private $username = ';
Private $password = ';
Private $port = 21;
Private $passive = TRUE;
Private $debug = TRUE;

Private $conn _id = FALSE; /** * constructor * @param array configuration: $config = array (' hostname ' => ', ' username ' => ', ' Password ' => ', ' Port ' => ' ...)
);

*/Public Function __construct ($config = Array ()) {if (count ($config) > 0) {$this->_init ($config);}} /** * FTP Connection * @access public * @param array * @return Boolean/Public function connect ($config = Array ()) {if (CO) UNT ($config) > 0) {$this->_init ($config);}//Determine if the FTP connection is open (FALSE = = ($this->conn_id = @ftp_connect ($this-&G T;hostname, $this->port)) {if ($this->debug = = TRUE) {$this->_error ("Ftp_uNable_to_connect ");
return FALSE; //Determine if login succeeded if (! $this->_login ()) {if ($this->debug = = TRUE) {$this->_error ("Ftp_unable_to_login");
FALSE;

//Determine whether to open the FTP passive mode if ($this->passive = = True) {FTP_PASV ($this->conn_id, True);}
return TRUE; /** * Directory Change * * @access public * @param string directory ID (FTP) * @param boolean * @return Boolean/Public function Chgdir ($path = ", $supress _DEBUG = FALSE)

{if ($path = = ' OR! $this->_isconn ()) {return FALSE;}

$result = @ftp_chdir ($this->conn_id, $path); if ($result = = False) {if ($this->debug = = TRUE and $supress _DEBUG = False) {$this->_error ("Ftp_unable_to_chgdir:
Dir[". $path."] ");}
return FALSE;
return TRUE; /** * Directory Generation * @access public * @param string directory ID (FTP) * @param int file Permissions List * @return Boolean/Public function Mkdi

R ($path = ', $permissions = NULL) {if ($path = = ' OR! $this->_isconn ()) {return FALSE;}

$result = @ftp_mkdir ($this->conn_id, $path); if ($result = = FALSE) {
if ($this->debug = = TRUE) {$this->_error ("ftp_unable_to_mkdir:dir[". $path. "]);

} if (! Is_null ($permissions)) {$this->chmod ($path, (int) $permissions);
return TRUE; /** * Upload * * @access public * @param string Local Directory Identity * @param string remote directory ID (FTP) * @param string upload mode auto | | ASCII * @param int uploaded file permissions list * @return Boolean/Public function upload ($localpath, $remotepath, $mode = ' auto ', $permi Ssions = NULL) {if (! $this->_isconn ()) {return FALSE;}//Determine if the local file exists if (! file_exists ($localpath)) {if ($this->d
Ebug = = TRUE) {$this->_error ("Ftp_no_source_file:". $localpath);}
return FALSE;
      //Judge upload mode if ($mode = = ' auto ') {//Get file suffix type $ext = $this->_getext ($localpath);
Depending on the suffix type, whether the upload mode is ftp_ascii (text mode) or ftp_binary (binary mode);
$mode = $this->_settype ($ext); $mode = ($mode = = ' ASCII ')?
Ftp_ascii:ftp_binary;
Upload $result = @ftp_put ($this->conn_id, $remotepath, $localpath, $mode); To determine if the upload succeeded if ($result = = FALSE) {if ($this->deBug = = TRUE) {$this->_error ("ftp_unable_to_upload:localpath[". $localpath. "]
/remotepath[". $remotepath."] ");}
return FALSE;

//Determine if you need to overwrite file permissions if (! Is_null ($permissions)) {$this->chmod ($remotepath, (int) $permissions);
return TRUE; /** * Download * * @access public * @param string remote directory Identification (FTP) * @param string local Directory ID * @param string download mode Auto | |  ASCII * @return Boolean/Public Function download ($remotepath, $localpath, $mode = ' auto ') {if (! $this->_isconn ())

{return FALSE;}

if ($mode = = ' auto ') {$ext = $this->_getext ($remotepath); $mode = $this->_settype ($ext);} $mode = ($mode = = ' ASCII ')?

Ftp_ascii:ftp_binary;

$result = @ftp_get ($this->conn_id, $localpath, $remotepath, $mode); if ($result = = FALSE) {if ($this->debug = = TRUE) {$this->_error ("ftp_unable_to_download:localpath[". $localpath ."]
-remotepath[". $remotepath."] ");}
return FALSE;
return TRUE; /** * Rename/move * * @access public * @param string remote directory Identification (FTP) * @param string new directory ID * @param boolEAN judge whether to rename (FALSE) or Move (TRUE) * @return Boolean/Public Function rename ($oldname, $newname, $move = FALSE) {if (! $this-

>_isconn ()) {return FALSE;}

$result = @ftp_rename ($this->conn_id, $oldname, $newname); if ($result = = False) {if ($this->debug = = TRUE) {$msg = ($move = = False)?
"Ftp_unable_to_rename": "Ftp_unable_to_move";
$this->_error ($msg);
return FALSE;
return TRUE;  /** * Delete File * * @access public * @param string file identification (FTP) * @return Boolean/Public Function Delete_file ($file) {if (!)

$this->_isconn ()) {return FALSE;}

$result = @ftp_delete ($this->conn_id, $file);
if ($result = = FALSE) {if ($this->debug = = TRUE) {$this->_error ("ftp_unable_to_delete_file:file[". $file. "]");
return FALSE;
return TRUE;  /** * Delete Folder * * @access public * @param string directory identification (FTP) * @return Boolean/Public Function Delete_dir ($path) {if (!)

$this->_isconn ()) {return FALSE;} Add a backslash ' \ ' $path = Preg_replace ("/(. +?) to the '/' character of the directory macro. \/*$/"," \\1/", $path);

Gets the directory file list $filelist = $this->filelist ($path); if ($filelist!== FALSE and Count ($filelist) > 0) {foreach ($filelist as $item) {//If we cannot delete it, then it may be a folder//So we recursively call Delet
E_dir () if (! @delete_file ($item)) {$this->delete_dir ($item);}

//Delete folder (empty folder) $result = @ftp_rmdir ($this->conn_id, $path);
if ($result = = FALSE) {if ($this->debug = = TRUE) {$this->_error ("ftp_unable_to_delete_dir:dir[". $path. "]");
return FALSE;
return TRUE; /** * Modify File Permissions * * @access public * @param string directory identification (FTP) * @return Boolean/Public function chmod ($path, $perm) {if (! $this->_isconn ())

{return FALSE;} Functions that modify permissions are defined only in PHP5 (FTP) if (! function_exists (' Ftp_chmod ')) {if ($this->debug = = TRUE) {$this->_error ("Ftp_
Unable_to_chmod (function) ");
return FALSE;

$result = @ftp_chmod ($this->conn_id, $perm, $path); if ($result = = FALSE) {if ($this->debug = = TRUE) {$this->_error ("ftp_unable_to_chmod:path[". $path. "]
-chmod[". $perm."] ");}
return FALSE; } return TRUE; /** * Get directory file list * * @access public * @param string directory ID (FTP) * @return Array/Public function filelist ($path = '. ')

{if (! $this->_isconn ()) {return FALSE;}
Return Ftp_nlist ($this->conn_id, $path); /** * Shut down FTP * * @access Public * @return Boolean/Public Function close () {if (! $this->_isconn ()) {return FALSE
;
Return @ftp_close ($this->conn_id); /** * FTP member variable initialization * * @access private * @param array configuration arrays * @return void */Private Function _init ($config = Array ())


{foreach ($config as $key => $val) {if (Isset ($this-> $key)) {$this-> $key = $val;}} Special character filter $this->hostname = preg_replace (' |. +?:/
/| ', ', $this->hostname); /** * FTP Login * * @access Private * @return Boolean/Private Function _login () {return @ftp_login ($this->conn_id, $
This->username, $this->password); /** * con_id * @access Private * @return Boolean/Private Function _isconn () {if (! Is_resource ($this->conn_i D)) {if ($this->debug = = TRUE) {$this->_error ("Ftp_no_connection");} return FALSE;
return TRUE; /** * get suffix extension from filename * * @access private * @param string directory ID * @return string/Private Function _getext ($filename) {if (FALSE = = = Strpos ($filename, '. '))

{return ' txt ';}
$extarr = Explode ('. ', $filename);
Return end ($extarr); /** * Defines FTP transfer mode from suffix extension ASCII or binary * * @access private * @param string suffix extension * @return string/Private Function _sett Ype ($ext) {$text _type = array (' txt ', ' text ', ' php ', ' Phps ', ' php4 ', ' js ', ' css ', ' htm ', ' html ', ' phtml ', ' shtml ', ' log ')

, ' xml '); Return (In_array ($ext, $text _type))?
' ASCII ': ' binary '; }/** * ERROR logging * @access Prvate * @return Boolean/Private Function _error ($msg) {return @file_put_contents (' Ftp_err . Log ', ' date['. Date ("Y-m-d h:i:s"). " -hostname[". $this->hostname."] -username[". $this->username."] -password[". $this->password."] -msg[". $msg."]
\ n ", file_append); }/*end of file ftp.php*//*location/apache group/htdocs/ftp.php*/

file 2:ftp_demo.php

<?php
require_once (' ftp.php ');


$config = Array (
' hostname ' => ' 101.64.183.92 ',//server address
' username ' => ' ftpadminuser ',  //ftp login account
' password ' => ' Admin_user ',   //ftp login password
' port ' => 2112         /port number
);


$FTP = new FTP ();
Connection
$ftp->connect ($config);
Upload the first parameter is the local file name, the second parameter is the FTP filename
$rs = $ftp->upload (' jsyh.flv ', ' jsyh.flv ');
if ($rs) {
  echo 1;
}
$ftp->download (' Ftp_upload.log ', ' ftp_download.log ');

The above is a small series for you to bring PHP with FTP class upload file video, such as the simple implementation of the full content of the method, I hope to help you, a lot of support cloud Habitat Community ~

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.