目录
回复讨论(解决方案)
首页 后端开发 php教程 php ftp上传多个文件时失败

php ftp上传多个文件时失败

Jun 23, 2016 pm 01:59 PM

遍历文件夹,打算批量上传到FTP上,li下有 dir和 files两个数组,一个是目录数组,一个文件数组
用ftp上传时失败,单个上传没有问题??? 怎么解决啊

foreach ($li['files'] as $i){	$c+=1;	$ftp->upload($i,$i);	if($c>= count($li['files'])/2)	{set_time_limit(300);}}
登录后复制


回复讨论(解决方案)

你有创建目录吗?

你有创建目录吗?
有啊有啊, 第一个文件好像可以,后续就不行了

我没看到,不好说

我没看到,不好说
这是从网上找的一个PHP FTP类 ,加了个遍历文件夹函数,单个文件上传没一点问题,如果用foreach的话连错误输出都没有,
VAR_DUMP也没输出,像下面这种单个的加好多条都没问题,能传上去,

$ftp->upload('smx/ewm/ewm.html','smx/ewm/ewm.html');
登录后复制

但像这个的,就连错误输出($this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");)都没
foreach ($li['files'] as $i){	$c+=1;	$r = $ftp->upload($i,$i);	if($r){		echo 'ok- '.$i.'<br/>';	}else{		echo 'fail- '.$i.'<br/>';	}}
登录后复制

?<?php/** * 仿写CodeIgniter的FTP类 * FTP基本操作: * 1) 登陆; 			connect * 2) 当前目录文件列表;  filelist * 3) 目录改变;			chgdir * 4) 重命名/移动;		rename * 5) 创建文件夹;		mkdir * 6) 删除;				delete_dir/delete_file * 7) 上传;				upload * 8) 下载				download * * @author quanshuidingdang */class Ftp {	private $hostname	= '';	private $username	= '';	private $password	= '';	private $port 		= 21;	private $passive 	= TRUE;	private $debug		= TRUE;	private $conn_id 	= FALSE;		/**	 * 构造函数	 *	 * @param	array	配置数组 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...);	 */	public function __construct($config = array()) {		if(count($config) > 0) {			$this->_init($config);		}	}		/**	 * FTP连接	 *	 * @access 	public	 * @param 	array 	配置数组	 * @return	boolean	 */	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;	}		/**	 * 目录改变	 *	 * @access 	public	 * @param 	string 	目录标识(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;	}		/**	 * 目录生成	 *	 * @access 	public	 * @param 	string 	目录标识(ftp)	 * @param	int  	文件权限列表		 * @return	boolean	 */	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;	}			/**	 * 生成批量目录,目录数组要排序	 *	 * @access 	public	 * @param 	string 	目录标识(ftp)	 * @param	int  	文件权限列表		 * @return	boolean	 */	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;	}			/**	 * 上传	 *	 * @access 	public	 * @param 	string 	本地目录标识	 * @param	string	远程目录标识(ftp)	 * @param	string	上传模式 auto || ascii	 * @param	int		上传后的文件权限列表		 * @return	boolean	 */	public function upload($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."]");  //这条错误输出都没			}			return FALSE;		}				if( ! is_null($permissions)) {			$this->chmod($remotepath,(int)$permissions);		}				return TRUE;	}			/**	 * 文件批量上传	 *	 * @access 	public	 * @param 	string 	本地目录标识	 * @param	string	远程目录标识(ftp)	 * @param	string	上传模式 auto || ascii	 * @param	int		上传后的文件权限列表		 * @return	boolean	 */	public function uppload($file) {		if( ! $this->_isconn()) {			return FALSE;		}				foreach($file as $i){			$this->upload($i,$i);		}				return TRUE;	}				/**	 * 关闭FTP	 *	 * @access 	public	 * @return	boolean	 */	public function close() {		if( ! $this->_isconn()) {			return FALSE;		}				return @ftp_close($this->conn_id);	}		/**	 * FTP成员变量初始化	 *	 * @access	private	 * @param	array	配置数组	 	 * @return	void	 */	private function _init($config = array()) {		foreach($config as $key => $val) {			if(isset($this->$key)) {				$this->$key = $val;			}		}		//特殊字符过滤		$this->hostname = preg_replace('|.+?://|','',$this->hostname);	}		/**	 * FTP登陆	 *	 * @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_id)) {			if($this->debug === TRUE) {				$this->_error("ftp_no_connection");			}			return FALSE;		}		return TRUE;	}		/**	 * 从文件名中获取后缀扩展	 *	 * @access 	private	 * @param 	string 	目录标识	 * @return	string	 */	private function _getext($filename) {		if(FALSE === strpos($filename, '.')) {			return 'txt';		}				$extarr = explode('.', $filename);		return end($extarr);	}		/**	 * 从后缀扩展定义FTP传输模式  ascii 或 binary	 *	 * @access 	private	 * @param 	string 	后缀扩展	 * @return	string	 */	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;    }		/**	 * 错误日志记录	 *	 * @access 	prvate	 * @return	boolean	 */	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*/
登录后复制

好了,是类 多次执行,覆盖了之前的,原生FTP操作 OK 

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
<🎜>掩盖:探险33-如何获得完美的色度催化剂
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1677
14
CakePHP 教程
1430
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? 说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型? PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型? Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP和Python:解释了不同的范例 PHP和Python:解释了不同的范例 Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

在PHP和Python之间进行选择:指南 在PHP和Python之间进行选择:指南 Apr 18, 2025 am 12:24 AM

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP和Python:深入了解他们的历史 PHP和Python:深入了解他们的历史 Apr 18, 2025 am 12:25 AM

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

PHP和框架:现代化语言 PHP和框架:现代化语言 Apr 18, 2025 am 12:14 AM

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

为什么要使用PHP?解释的优点和好处 为什么要使用PHP?解释的优点和好处 Apr 16, 2025 am 12:16 AM

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。

PHP的影响:网络开发及以后 PHP的影响:网络开发及以后 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles