PHP FTP failed when uploading multiple files

Source: Internet
Author: User
Tags ftp connection ftp login ftp transfer
Traverse the folder, intend to upload to the FTP batch, Li under the Dir and files two arrays, one is a directory array, an array of files
Upload with FTP failed, single upload no problem??? How to solve it?
foreach ($li [' Files '] as $i) {$c +=1; $ftp->upload ($i, $i); if ($c >= count ($li [' Files '])/2) {Set_time_limit (300)}}


Reply to discussion (solution)

Did you create a directory?

Did you create a directory?
Yes, well, the first file seems to be possible, and the next one is gone.

I don't see it, I'm not.

I don't see it, I'm not.
This is from the Internet to find a PHP FTP class, add a traversal folder function, a single file upload no problem, if you use foreach even error output is not,
Var_dump also not output, like the following this single plus a lot of strips are no problem, can pass up,

$ftp->upload (' smx/ewm/ewm.html ', ' smx/ewm/ewm.html ');

But like this, even the error output ($this->_error ("ftp_unable_to_upload:localpath[". $localpath. "] /remotepath[". $remotepath."] ");) Are not
foreach ($li [' Files '] as $i) {$c +=1; $r = $ftp->upload ($i, $i), if ($r) {echo ' ok-'. $i. '
';} Else{echo ' fail-'. $i. '
';}}

?
 ', ' username ' + ', ' password ' + ', ' port ' = ' ... '; */public function __construct ($config = Array ()) {if (count ($config) > 0) {$this->_init ($config);}} /** * FTP Connection * * @access public * @param array Configuration array * @returnboolean */public function Connect ($config = Array ()) {if (count ($config) > 0) {$this->_init ($config);} if (FALSE = = = ($this->conn_id = @ftp_connect ($this->hostname, $this->port)) {if ($this->debug = = = TRUE) {$ This->_error ("Ftp_unable_to_connect");} return FALSE;} if (! $this->_login ()) {if ($this->debug = = = TRUE) {$this->_error ("Ftp_unable_to_login");} return FALSE;} if ($this->passive = = = True) {FTP_PASV ($this->conn_id, True);} return TRUE;} /** * Directory Change * * @access public * @param string directory identifier (FTP) * @paramboolean * @returnboolean */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) * @paramint file permission list * @returnboolean */public function mkdir ($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."] "); return FALSE;} if (! Is_null ($permissions)) {$this->chmod ($path, (int) $permissions);} return TRUE;} /** * Generate batch catalogs, directory array to sort * * @access public * @param string directory ID (FTP) * @paramint file permission list * @returnboolean */public function M Kpdir ($path = Array ()) {if ($path = = "OR! $this->_isconn ()) {return FALSE;} foreach ($path as $d) {$result = @ftp_mkdir ($this->conn_id, $d), if ($result = = = FALSE) {if ($this->debug = = = TRUE) {$th Is->_error ("ftp_unable_to_mkpdir:dir[". $path. "]"); return FALSE;}} return TRUE;} /** * Upload * * @access Public * @param string Local directory identity * @paramstring remote directory Identity (FTP) * @paramstring upload mode auto | | ASCII * @paramint file permission list after uploading * @returnboolean */public function upload ($localpath, $remotepath, $mode = ' auto ', $permission s = NULL) {if (! $this->_isconn ()) {return FALSE;} if (! file_exists ($localpath)) {if ($this->debug = = = TRUE) {$this->_error ("Ftp_no_source_file:". $localpath);} return FALSE;} if ($mode = = ' auto ') {$ext = $this->_getext ($localpath); $mode = $this->_settype ($ext);} $mode = ($mode = = ' ASCII ')? ftp_ascii:ftp_binary; $result = @ftp_put ($this->conn_id, $remotepath, $localpath, $mode); if ($result = = = FALSE) {if ($ This->debug = = = TRUE) {$this->_error ("ftp_unable_to_upload:localpath[". $localpath. "]  /remotepath[". $remotepath."] "); This error output is not}return FALSE;} if (! Is_null ($permissions)) {$this->chmod ($remotepath, (int) $permissions);} return TRUE;} /** * File Bulk Upload * * @access public * @param string Local directory identity * @paramstring remote directory ID (FTP) * @paramstring upload mode auto | | ASCII * @paramint file permissions after uploadingList * @returnboolean */public function uppload ($file) {if (! $this->_isconn ()) {return FALSE;} foreach ($file as $i) {$this->upload ($i, $i);} return TRUE;} /** * Close FTP * * @access public * @returnboolean */public function Close () {if (! $this->_isconn ()) {return FALSE;} Return @ftp_close ($this->conn_id);} /** * FTP member variable initialization * * @accessprivate * @paramarray configuration array * @returnvoid */private function _init ($config = Array ()) {foreach ( $config as $key + $val) {if (Isset ($this-$key)) {$this, $key = $val;}} special character Filtering $this->hostname = Preg_replace (' |. +?:/ /| ', ', $this->hostname);} /** * FTP Login * * @access private * @returnboolean */private function _login () {return @ftp_login ($this->conn_id, $this-& Gt;username, $this->password);} /** * Judge Con_id * * @access private * @returnboolean */private function _isconn () {if (! Is_resource ($this->conn_id)) {i F ($this->debug = = = TRUE) {$this->_error ("Ftp_no_connection");} return FALSE;} return TRUE;} /** * Get suffix extension from file name * * @access Private* @param string Directory ID * @returnstring */private function _getext ($filename) {if (FALSE = = = Strpos ($filename, '. ')) {return ' txt ';} $extarr = Explode ('. ', $filename); return end ($extarr);} /** * Definition of FTP transfer mode from suffix extension ASCII or binary * * @access private * @param string suffix extension * @returnstring */private function _settype ( $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 ';}        function Listdir ($dirname) {Static $r = Array (' dir ' = = = Array (), ' files ' = = Array (),);        $dir = Opendir ($dirname); while ($file = Readdir ($dir))! = False) {if ($file = = "." | | $file = = "..") {continue;} if (Is_dir ($dirname. "/" . $file) {Array_push ($r [' dir '], $dirname. "/" . $file); $this->listdir ($dirname. "/" .            $file); } else {Array_push ($r [' Files '], $dirname. "/" . $file); $this->upload ($dirname. "/" . $file, $dirname. "/" .    $file);        }}return $r; }/** * ERROR Log record * * @access prvate * @returnboolean */private function _error ($msg) {if (GetType ($msg) = = ' array ') {return @fi Le_put_contents (' Ftp_err.log ', "date[". Date ("Y-m-d h:i:s"). "] -hostname[". $this->hostname."] -username[". $this->username."] -password[". $this->password."] -msg[". Implode ('-', $msg)." \ n ", file_append);} Else{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*/

Well, is the class executes multiple times, overwriting the previous, native FTP operation OK

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