欢迎光临
感谢一路有你

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

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 

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

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

以下附源码

上传类

<?php
//上传类
class UploadFile {
	public $maxSize = 10485760; // 上传文件的最大值,默认10M
	public $allowExts = array();	//允许的文件后缀
	
	public $savePath ='';	// 上传文件保存路径
	public $saveRule = 'md5_file'; //命名规则
	public $autoSave = true;	//自动保存,设置为false,自定义存储方式
	public $domain = 'public';
	
	public $thumb = false; //是否开启缩略图
	public $thumbMaxWidth = 100;	//缩略图最大宽度
	public $thumbMaxHeight = 100;	//缩略图最大高度   
	public $thumbPrefix = 'thumb_'; //缩略图前缀
	public $thumbPath = '';  //缩略图保存路径,为空则为上传文件保存路径savePath
	
	protected $saeStorage = NULL;
	protected $uploadFileInfo = array(); //上传成功的文件信息
	protected $errorMsg = ''; //错误信息
	
	public function __construct($savePath="public/uploads/", $allowExts = array('gif','jpg','jpeg','bmp','png'), $maxSize = 10485760) {
		$this->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 "<pre>";
			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) 打赏
未经允许不得转载:王明昌博客 » 上传图片时,提示move_uploaded_file错误/chomd函数
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏