总会遇到一些问题,比如上传图片,我采用thinkphp来作为服务端操作,上代码:
Public function upload($folderName) {
import('ORG.Net.UploadFile');
$upload = new \Think\Upload(); // 实例化上传类
$upload->replace = true;
$upload->maxSize = 5242880; // 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg','bmp'); // 设置附件上传类型
$upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
$upload->autoSub = true;//设置是否自动创建文件夹
$upload->subName = $folderName?$folderName:array('date','Y-m-d');
// 上传文件
$info = $upload->upload();
$path = $upload->rootPath;
$data['msg']='';
if (!$info) {// 上传错误提示错误信息
$data['ok'] = false;
$data['msg']=$upload->getError();
} else {// 上传成功
$savefile = $info['file'];
$path.=$savefile['savepath'] . $savefile['savename'];
$data['msg'] = '上传成功';
$data['ok'] = true;
$data['path'] = $path;
//$this->success('上传成功!');
}
return $data;
// $this->ajaxReturn($data);
}
/**
* 删除上传文件
* @param type $filename
* @return type
*/
public function delFile($filename){
return unlink($filename);
}
/**
* 根据文件目录循环文件名
*/
public function openCktp() {
$dir = opendir('./Public/Uploads/cktp');
$list = array();
while ($file = readdir($dir)) {
if (($file == '.' || $file == '..')) {
continue;
}
array_push($list, $file);
}
closedir($dir);
$this->ajaxReturn($list);
}
百度已收录
记录一次php图片上传浏览删除操作:等您坐沙发呢!