Maison >développement back-end >tutoriel php >图片以路径方式存入数据库 数据库设计 以及在页面中显示 急 !解决办法
图片以路径方式存入数据库 数据库设计 以及在页面中显示 急急急 !
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);
echo "上传成功";
echo "$name.
";
echo "$type.
";
echo "$size.
";
echo "$tmp_name.
";
}
}
?>
// 需要传递用户名,或者上传者的用户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.<br>"; echo "$type.<br>"; echo "$size.<br>"; echo "$tmp_name.<br>"; } else { echo '数据更新失败'; } }}<div class="clear"> </div>