e * @param string $uploadDirectory * @param string $replaceOldFile=true * @returns array(''success''=>true) or array(''error''=>''error message'') */ function handleUpload($uploadDirectory, $replaceOldFile = FALSE){ if (!is_writable($uploadDirectory)){ return array(''error'' => "Server error. Upload directory isn''t writable."); }
if (!$this->file){ return array(''error'' => ''No files were uploaded.''); }
$size = $this->file->getSize();
if ($size == 0) { return array(''error'' => ''File is empty''); }
if ($size > $this->sizeLimit) { return array(''error'' => ''File is too large''); }
$pathinfo = pathinfo($this->file->getName()); $filename = $pathinfo[''filename'']; //$filename = md5(uniqid()); $ext = @$pathinfo[''extension''];// hide notices if extension is empty
if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){ $these = implode('', '', $this->allowedExtensions); return array(''error'' => ''File has an invalid extension, it should be one of ''. $these . ''.''); }
$ext = ($ext == '''') ? $ext : ''.'' . $ext;
if(!$replaceOldFile){ /// don''t overwrite previous files that were uploaded while (file_exists($uploadDirectory . DIRECTORY_SEPARATOR . $filename . $ext)) { $filename .= rand(10, 99); } }
$this->uploadName = $filename . $ext;
if ($this->file->save($uploadDirectory . DIRECTORY_SEPARATOR . $filename . $ext)){ return array(''success''=>true); } else { return array(''error''=> ''Could not save uploaded file.'' . ''The upload was cancelled, or server error encountered''); }
}
在调用这个处理函数时.需要注意的是.传递的URL存储路径需要时绝对的.所以需要对传入路劲做一下格式化处理: $uploadDirectory =
Fine Uploader文件上传组件应用介绍_网学
在上传操作过程发信很多出现“increase post_max_size and upload_max_filesize to 10M”错误,其实针对这个问题.主要是上传文件配置超过php环境默认的2M.需要在php.ini文件中把post_max_size和upload_max_filesize两项的值改到10M以上,然后重启Apache即可.或是参考Php官网针对配置说明 修改php.ini配置文件. 至此整个Fine Uploader配置流程已经全部完成.点击选择文件时.会如下效果: