一般图片都存放在本地硬盘中,这样可以减轻服务器传输的负担。具体如下:
/* ------------------ html ------------------ */
/* ------------------ php ------------------ */
//上传用户头像
function uploadImg(){
//客户端机器文件的原名称
$userFile = $_FILES['userImg'];
echo "userfile-->>".$userFile."
";
//文件被上传后在服务端储存的临时文件名
$tmp_file = $_FILES['userImg']['tmp_name'];
echo "tmp_file-->>".$tmp_file."
";
$file_dir = "../../photos/";
$uploadfile = $file_dir . basename($_FILES['userImg']['name']);
echo "upLoadFile-->>".$uploadfile."
";
/* 将上传文件移至photos文件夹中
$a = move_uploaded_file($tmp_file, $uploadfile);
if($a == true){
echo "上传成功!
";
return $uploadfile;
}else{
echo "上传失败!
";
}
*/
}
//上传用户头像
function insertImg(){
$userImg = uploadImg();
$sql = "INSERT INTO lu_user(userImg) VALUES(?);";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s",$userImg);
$stmt->execute();
$rows = $stmt->affected_rows;
if ($rows > 0){
echo "成功!
"; }else{
echo "失败!
";
}
}
插到库中的图片路径->$uploadfile;
"../../photos/图片名.jpg"