加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
imageUploadForm.php 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
845758376 提交于 2019-12-06 11:51 . php仿微博
<?php
//判断上传文件是否满足需求
if (!$_FILES['Filedata']) {
die ( 'Image data not detected!' );
}
if ($_FILES['Filedata']['error'] > 0) {
switch ($_FILES ['Filedata'] ['error']) {
case 1 :
$error_log = 'The file is bigger than this PHP installation allows';
break;
case 2 :
$error_log = 'The file is bigger than this form allows';
break;
case 3 :
$error_log = 'Only part of the file was uploaded';
break;
case 4 :
$error_log = 'No file was uploaded';
break;
default :
break;
}
die ( 'upload error:' . $error_log );
} else {
$img_data = $_FILES['Filedata']['tmp_name'];
$size = getimagesize($img_data);
$file_type = $size['mime'];
if (!in_array($file_type, array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))) {
$error_log = 'only allow jpg,png,gif';
die ( 'upload error:' . $error_log );
}
switch($file_type) {
case 'image/jpg' :
case 'image/jpeg' :
case 'image/pjpeg' :
$extension = 'jpg';
break;
case 'image/png' :
$extension = 'png';
break;
case 'image/gif' :
$extension = 'gif';
break;
}
}
if (!is_file($img_data)) {
die ( 'Image upload error!' );
}
//图片保存路径,默认保存在该代码所在目录(可根据实际需求修改保存路径)
$save_path = dirname( __FILE__ ).'/public/images/upload/head_image';
$uinqid = uniqid();
$filename = $save_path . '/' . $uinqid . '.' . $extension;
$result = move_uploaded_file( $img_data, $filename );
if ( ! $result || ! is_file( $filename ) ) {
die ( 'Image upload error!' );
}
echo 'Image data save successed,file:' . $filename;
require('library/Db.class.php');//连接数据库
session_start();//开启session
$user_id = $_SESSION['user']['id'];//获取登录用户id
$db = new Db();
$head = $uinqid . '.' . $extension;//组装路径
$sql = "UPDATE mr_user SET avatar = :head WHERE id = :user_id";
$res = $db->query($sql,array("user_id"=>$user_id,"head"=>$head));//更改头像
$_SESSION['user']['avatar'] = $head;//更改头像session信息
exit ();
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化