时间:2021-07-01 10:21:17 帮助过:7人阅读
// 需要传递用户名,或者上传者的用户ID,可以用session来控制,看你的登录代码怎么写 // 假设为session 如果是GET id 请自行修改其他页面传递id 并且做赋值 session_start (); // 赋值变量 $Uid = $_SESSION ['UserId']; if (is_uploaded_file ( $_FILES ['upfile'] ['tmp_name'] )) { $upfile = $_FILES ["upfile"]; $name = $upfile ["name"]; $type = $upfile ["type"]; $size = $upfile ["size"]; $tmp_name = $upfile ["tmp_name"]; $error = $upfile ["error"]; switch ($type) { case 'image/pjpeg' : $ok = 1; break; case 'image/jpeg' : $ok = 1; break; case 'image/gif' : $ok = 1; break; case 'image/png' : $ok = 1; break; } if ($ok && $error == '0') { move_uploaded_file ( $tmp_name, 'up/' . $name ); // 将文件名保存到数据库 // 数据库链接部分,自行修正完整 mysql_connect (); //更新数据库字段img 查询条件 用户id $SQL = "UPDATE zuoye SET `img`='{$name}' WHERE `id`='{$Uid}'"; mysql_query ( $SQL ); if (mysql_affected_rows () > 0) { echo "上传头像数据更新成功"; echo "$name.
"; echo "$type.
"; echo "$size.
"; echo "$tmp_name.
"; } else { echo '数据更新失败'; } } }