上傳圖片到伺服器,並且重新命名圖片-更改(小白共勉)
阿新 • • 發佈:2018-12-09
實現使用者註冊功能,使用者上傳頭像放在imgs的目錄下,所涉及檔案分別是register.html(註冊),conn.php(連線資料庫),img.php(上傳圖片與更改圖片名稱,改成了是一個函式,返回 圖片儲存位置+圖片名稱),register.php(註冊功能)
register.html介面如下:
<form action="../php/registeradmin.php" method="post" enctype="multipart/form-data"> <div class="logindiv">使用者姓名:<input type="text" name="user" placeholder="使用者名稱" /></div> <div class="logindiv">使用者密碼:<input type="password" name="password" placeholder="密碼" /></div> <div class="logindiv">聯絡方式:<input type="phone" name="phone" placeholder="聯絡方式" /></div> <div class="logindiv">使用者郵箱:<input type="email" name="email" placeholder="使用者郵箱" ></div> <div class="logindiv">聯絡地址:<input type="text" name="address" placeholder="聯絡地址" /></div> <div class="logindiv">使用者頭像:<input type="file" name="file"/></div> <input type="submit" id="createadmin" value="建立超級管理員" /> </form>
conn.php不貼,img.php如下:
<?php function uploadimg($imgnewname){ // 允許上傳的圖片字尾 $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["file"]["name"]); $extension = end($temp); // 獲取檔案字尾名 if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 204800) // 小於 200 kb && in_array($extension, $allowedExts)){ if ($_FILES["file"]["error"] > 0){ echo "錯誤:: " . $_FILES["file"]["error"] . "<br>"; }else{ // 上傳圖片的路徑 $path = "../imgs"; // 獲取圖片真實名稱 $imgname = $_FILES["file"]["name"]; // 圖片新名字,使用者名稱.png/.jpg等 $imgnew = $imgnewname . "." . $extension; // 使用者需要儲存的路徑+圖片名 $userimg = $path."/".$imgnew; echo $userimg; // 重新命名,此處報錯,需要修改 rename(iconv('UTF-8', 'GBK',$imgname), iconv('UTF-8','GBK',$imgnew)); // 如果 upload 目錄不存在該檔案則將檔案上傳到 imgs 目錄下 move_uploaded_file($_FILES["file"]["tmp_name"], $userimg); // 傳遞給呼叫者儲存位置內容 return $userimg; } }else{ echo "非法的檔案格式"; } } ?>
register.php如下:
<?php // 超級使用者註冊功能 // 連線資料庫 include "connect.php"; // 獲取所上傳圖片路徑 include "img.php"; $user = $_POST['user']; $password1 = $_POST['password']; $phone = $_POST['phone']; $email = $_POST['email']; $address = $_POST['address']; // 密碼加密 $password = password_hash($password1, PASSWORD_DEFAULT); $credit = "5"; $manner = "1"; $userclass = "0"; // 訪問img.php下的uploadimg函式,上傳圖片,返回上傳路勁,如下為重新命名為$user所代表內容, $userimg = uploadimg($user); $createtime = date("Y/m/d h:i:s"); // 判斷是否已經存在將要註冊的使用者名稱 $sqlsearch = "select * from user where user='$user'"; $seresult = $conn->query($sqlsearch); if($seresult->num_rows>0){ echo "該使用者名稱已經存在,請輸入其他使用者名稱"; // header("Location:../admin/create.html"); }else{ echo "沒有該使用者資訊"; $sqlinsert = "INSERT INTO user (user, password, phone, email, address, credit, manner, userclass, userimg, createtime) VALUES ('".$user."','".$password."','".$phone."','".$email."','".$address."','".$credit."', '".$manner."', '".$userclass."', '".$userimg."', '".$createtime."')"; if ($conn->query($sqlinsert) === TRUE) { echo "新記錄插入成功"; // header("Location:../admin/index.html"); } else { echo "出錯,重新註冊 " . $sql . "<br>" . $conn->error; // header("Location:../admin/create.html"); } } ?>
不要在意註釋掉的程式碼,那是我在測試其他方式