Phpftp fails to traverse folders when uploading multiple files. it is intended to upload files to FTP in batches. There are two arrays, dir and files, one is the directory array and the other is the file array.
Failed to upload via ftp. there is no problem with a single upload ??? How can this problem be solved?
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)
Have you created a directory?
Have you created a directory?
Yes. The first file seems to be okay, but it will not work in the future.
I didn't see it.
I didn't see it.
This is a php ftp class found on the internet. a folder Traversal function is added. uploading a single file is fine. if foreach is used, no error output is returned,
VAR_DUMP has no output either. it is okay to add a number of entries such as below. it can be uploaded,
$ftp->upload('smx/ewm/ewm.html','smx/ewm/ewm.html');
However, even the error output ($ this-> _ error ("ftp_unable_to_upload: localpath [". $ localpath. "]/remotepath [". $ remotepath. "]");) none
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 configure 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 ID (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 identifier (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 directories, sort the directory array ** @ access public * @ param string Directory identity (ftp) * @ paramint file permission list * @ returnboolean */public function mkpdir ($ 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) {$ this-> _ error ("ftp_unable_to_mkpdir: dir [". $ path. "]");} return FALSE;} return TRUE ;} /*** Upload ** @ access public * @ param string local Directory ID * @ paramstring remote Directory ID (ftp) * @ paramstring Upload mode auto | ascii * @ paramint list of uploaded file permissions * @ returnboolean */public function uploa D ($ localpath, $ remotepath, $ mode = 'auto', $ permissions = 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 does not have} return FALSE;} if (! Is_null ($ permissions) {$ this-> chmod ($ remotepath, (int) $ permissions);} return TRUE ;} /*** batch upload of files ** @ access public * @ param string local Directory ID * @ paramstring remote Directory ID (ftp) * @ paramstring Upload mode auto | ascii * @ paramint list of uploaded file permissions * @ returnboolean */public function uppload ($ file) {if (! $ This-> _ isconn () {return FALSE;} foreach ($ file as $ I) {$ this-> upload ($ I, $ I );} return TRUE;}/*** disable 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-> username, $ this-> password );} /*** determine the con_id ** @ access private * @ returnboolean */private function _ isconn () {if (! Is_resource ($ this-> conn_id) {if ($ this-> debug === TRUE) {$ this-> _ error ("ftp_no_connection");} return FALSE ;} return TRUE;}/*** get extension suffix 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);}/*** extended definition of FTP transmission mode ascii or binary ** @ access private *@ Param string extension * @ returnstring */private function _ settype ($ ext) {$ text_type = array ('txt ', 'text', 'php', 'phps ', 'php4 ', 'js', 'css', 'htm', 'html', 'phpml', '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 @ file_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 */
OK