又折腾大半天,最后原因竟然出在路径上
require_once realpath(dirname(__FILE__) . '/../../../../') . '/vendor/qiniu/php-sdk/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
不太懂这里为什么要用realpath(),有没有路过的大佬帮忙讲解下,在此先行谢过
回归正题,直接上代码
先找到Uploader.class.php 这个文件
/**
* 上传文件的主处理方法
* @return mixed
*/
private function upFile()
$accessKey = 'youe qiniu accessKey';
$secretKey = 'youe qiniu secretKey';
$bucket = 'youe qiniu bucket';
// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
// 生成上传 Token
$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();
$file = $this->file = $_FILES[$this->fileField];
if (!$file) {
$this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND");
return;
}
if ($this->file['error']) {
$this->stateInfo = $this->getStateInfo($file['error']);
return;
} else if (!file_exists($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND");
return;
} else if (!is_uploaded_file($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMPFILE");
return;
}
$this->oriName = $file['name'];
$this->fileSize = $file['size'];
$this->fileType = $this->getFileExt();
// $this->fullName = $this->getFullName();
$this->filePath = $file['tmp_name'];
// $this->fileName = $this->getFullName();
// $dirname = dirname($this->filePath);
//检查文件大小是否超出限制
if (!$this->checkSize()) {
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
return;
}
//检查是否不允许的文件格式
if (!$this->checkType()) {
$this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
return;
}
$rand_str = rand(10000, 99999);
//新文件名
$this->fileName = date("YmdHis") . '_' . $rand_str . $this->fileType;
list($ret, $err) = $uploadMgr->putFile($token, $this->fileName, $this->filePath);
if ($err !== null) {
$this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");//
} else {
/// $result = array('url'=>IMG_URL.$ret['key']);
$this->stateInfo = $this->stateMap[0];
$this->fullName = 'http://img.xxx.com/' . $ret['key'];
} echo json_encode(array('error' => 0, 'url' => IMG_URL.$ret['key']));// }
// $this->oriName = $file['name'];
$this->fileSize = $file['size'];
/// // $this->fileType = $this->getFileExt();
/// // $this->filePath = $file['tmp_name'];
$this->fileName = "1.jpg";
// //创建目录失败
// if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
// $this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
// return;
// } else if (!is_writeable($dirname)) {
// $this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
// return;
// }
// //移动文件
// if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移动失败
// $this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");
// } else { //移动成功
// $this->stateInfo = $this->stateMap[0];
// }
}