限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。
当前位置:首页 >开发者 >PHP笔记 >PHP高级

上传图片时,提示move_uploaded_file错误/chomd函数

分类:PHP高级时间:2018-06-29浏览:3323

查看了下,原来服务器禁用了chmod()函数 上传文件连读的权限都没有

解除禁用后还是报这个错误,还是上传转移文件的问题,最后解决了原来是服务器空间不足了,导致转移失败,,,真是太无语了

以下附源码

上传类

savePath = $savePath;
		$this->allowExts = $allowExts;
		$this->maxSize = $maxSize;
		if( class_exists('SaeStorage') ) {
			$this->saeStorage = new SaeStorage();
		}		
	}
	
	public function upload($key='') {
		if( empty($_FILES) ) {
			$this->errorMsg = '没有文件上传!';
			return false;
		}
		if( empty($key) ) {
			$files = $_FILES;
		} else {
			$files[$key] = $_FILES[$key];
		}
		
		$num = 0;		
		foreach($files as $key =>$file) {
			if( $file['error'] == 4 ) continue;
			$saveRuleFunc = $this->saveRule;
			$pathinfo = pathinfo($file['name']);
			
			$file['key'] = $key;
			$file['extension'] = strtolower( $pathinfo['extension'] );
			$file['savepath'] = $this->savePath;
			$file['savename'] = $saveRuleFunc( $file['tmp_name'] ) . '.' . $file['extension'];
			//检查文件类型大小和合法性
			if ( !$this->check($file) ) {
				return false;
			}
			//存储文件
			if ( $this->autoSave ) {
				if( isset($this->saeStorage) ) {
					$file['savepath'] = str_replace(array('../', './'), '', $file['savepath']);
					$ret = $this->saeSave($file['tmp_name'], $file['savepath'] . $file['savename']);
				} else {
					$ret = $this->localSave($file['tmp_name'], $file['savepath'] . $file['savename']);
				} 
				if( !$ret ) {
					return false;
				}			
				$this->thumb($file); //缩略图片
			}
			
			$this->uploadFileInfo[$key] = $file;
			$this->uploadFileInfo[$num++] = $file;
		}
		return true;
	}
	
	//检查文件类型大小和合法性
	protected function check($file) {
		//文件上传失败
		if($file['error'] !== 0) {
			$this->errorMsg= '文件上传失败!';
			return false;
		}	
		//检查文件类型
		$this->allowExts = array_map('strtolower', $this->allowExts);		
		if( !in_array($file['extension'], $this->allowExts) ) {
			$this->errorMsg = '上传文件类型不允许!';
			return false;
		}
		//检查文件大小
		if ( $file['size'] > $this->maxSize ) {
			$this->errorMsg = '上传文件大小超出限制!';
			return false;
		}
		//检查是否合法上传
		if(!is_uploaded_file($file['tmp_name'])) {
			$this->errorMsg = '非法上传文件!';
			return false;
		}
		// 如果是图像文件 检测文件格式
		if( in_array($file['extension'], array('gif','jpg','jpeg','bmp','png','swf')) && false === getimagesize($file['tmp_name']) ) {
			$this->errorMsg = '非法图像文件';
			return false;
		}
		//检查通过,返回true
		return true;
	}
		
	//缩略图片
	protected function thumb($file) {
		if ($this->thumb && in_array($file['extension'], array('gif', 'jpg', 'jpeg', 'bmp', 'png')) ) {
			$this->thumbPath = $this->thumbPath ? $this->thumbPath : $file['savepath'];
			$thumbname = $this->thumbPath . $this->thumbPrefix . basename($file['savename']);
			require_once(dirname(__FILE__).'/Image.class.php');	
			$imagename =  isset($this->saeStorage) ? $file['tmp_name'] : $file['savepath'] . $file['savename'];
			return Image::thumb($imagename, $thumbname, $this->domain, $this->thumbMaxWidth, $this->thumbMaxHeight); // 生成图像缩略图
		}
		return false;
	}
		
	//sae存储
	protected function saeSave($srcFileName, $destFileName) {
		if ( false!= $this->saeStorage->upload($this->domain, $destFileName, $srcFileName) ) {
			 return true;
		} else {
			$this->errorMsg = $this->saeStorage->errmsg();
			return false;
		}
	}

	//本地存储
	protected function localSave($srcFileName, $destFileName) {
		$dir = dirname($destFileName);
		if ( !is_dir($dir) ) {
			if ( !mkdir($dir, 0777, true) ) {
				$this->errorMsg = '上传目录' . $dir . '不存在';
				return false;
			}
		} else {
			if ( !is_writeable($dir) ) {
				$this->errorMsg = '上传目录' . $dir . '不可写';
				return false;
			 }
		}
		echo "
";
			var_dump($srcFileName);die;
		if( move_uploaded_file($srcFileName, $destFileName) ) {
			return true;
		} 
		$this->errorMsg= '文件上传保存错误!';
		return false;
	}
	
	//上传成功获取返回信息
	public function getUploadFileInfo() {
		return $this->uploadFileInfo;
	}
	
	//获取错误信息
	public function getErrorMsg() {
		return $this->errorMsg;
	}
}
?>
上传方法
public function upload() {
        //解决部分浏览器swfupload新建线程的问题
        if (!empty($_POST['PHPSESSID'])) {
            session_id(in($_POST['PHPSESSID']));
            session_start();
        }
        $upload_dir = $_SESSION['formtoken'];
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 1024 * 1024 * 20; //最大2M
        //定义允许上传的文件扩展名
        $ext_arr = array(
            'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
            'flash' => array('swf', 'flv'),
            'media' => array('swf', 'flv', 'mp4', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
            'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'zip', 'rar', '7z'),
        );
        //设置上传文件类型
        $dir_name = empty($_GET['dir']) ? 'image' : in($_GET['dir']);
        if ($dir_name == 'image') {
            $upload->thumb = false; //开启缩略图
            $upload->thumbMaxWidth = 120;
            $upload->thumbMaxHeight = 90;
        }
        $upload->allowExts = $ext_arr[$dir_name];
        //设置附件上传目录
        $upload->savePath = $this->config['site_attached']. $upload_dir . '/' . $dir_name . '/';
        $upload->saveRule = cp_uniqid;
        @chmod($upload->savePath, 0777); //设置权限
        if (!$upload->upload()) {
            //捕获上传异常
            echo json_encode(array('error' => 1, 'message' => $upload->getErrorMsg()));
        } else {
            //取得成功上传的文件信息
            $fileinfo = $upload->getUploadFileInfo();
            echo json_encode(array("error" => 0, "url" => __ROOT__ . '/' . $upload->savePath . $fileinfo['imgFile']['savename']));
        }
    }
本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
0/500
Share your thoughts respectfully.