2017-12-03 274 views
-1

當我嘗試上傳圖像時,其獲取空字段。我不知道爲什麼。 請幫助..無法獲取圖像上傳到MySQL longblob

HTML表單:

<form action="edit_profile.php" method="POST" enctype="multipart/formdata"> 
    <input type="file" name="profile_image" required=""> 
    <button name="upload_image" class="btn btn-danger">Go</button> 
</form> 

PHP:

if(isset($_POST['upload_image'])){ 
     $image = $_FILES['profile_image']['tmp_name']; 
     $profile_image = addslashes(file_get_contents($image)); 

     $sql = "update users set image='".$profile_image."' where username='$username'"; 
     $result = mysqli_query($con, $sql); 

     if($result){ 
      echo "<script>window.open('profile.php','_SELF')</script>"; 
     } 
     else{ 
      echo "<script>alert('Error!')</script>"; 
      echo "<script>window.open('profile.php','_SELF')</script>"; 
     } 
    } 

我漸漸空虛像場..

+0

表單的''enctype''應該是''multipart/form-data'',而不是''multipart/formdata''。可能重複的https://stackoverflow.com/questions/35417567/uploading-an-image-to-a-mysql-database-using-a-blob – kmoser

+0

@Subham,你聲明的$用戶名?還有一件事你必須在按鈕中使用type =「submit」 –

+0

@NarendraVerma $ username是我的會話用戶名,我忘了編輯這個問題。如果我使用而不是

回答

-1

試試這個

你必須從更改至enctype="multipart/form-data"

我將type="submit"添加到按鈕。我知道這是在默認情況下submit

HTML代碼

<form action="process.php" method="post" enctype="multipart/form-data"> 
    <input type="file" name="profile_image" required=""> 
    <button type="submit" name="upload_image" class="btn btn-danger">Go</button> 
</form> 

Process.php

添加您的數據庫文件在這裏

if (isset($_POST["upload_image"])) { 
if ($_FILES["profile_image"]["error"] > 0) { 
      $error = $_FILES["file"]["error"]; 
     } 
      else if (($_FILES["profile_image"]["type"] == "image/gif") || 
      ($_FILES["profile_image"]["type"] == "image/jpeg") || 
      ($_FILES["profile_image"]["type"] == "image/png") || 
      ($_FILES["profile_image"]["type"] == "image/pjpeg")) { 

      $temp = explode(".", $_FILES["profile_image"]["name"]); 
      $newfilename = round(microtime(true)) . '.' . end($temp); 
      //echo $newfilename; 
      $url = 'upload/'.$newfilename;// it will store the image in the upload folder 
      $filename = compress_image($_FILES["profile_image"]["tmp_name"], $url, 80);//this will call the compress_image fruntion for optimize the image 
     }else { 
      $error = "Uploaded image should be jpg or gif or png"; 
     } 

$new_image_name=$newfilename;//here is the new random file name 

$sql = "update users set image='".$new_image_name."' where username='$username'"; 
     $result = mysqli_query($conn, $sql); 
     if($result){ 
      echo "<script>window.open('profile.php','_SELF')</script>"; 
     } 
     else{ 
      echo "<script>alert('Error!')</script>"; 
      echo "<script>window.open('profile.php','_SELF')</script>"; 
     } 
} 

以下功能將優化圖像。

$name = ''; $type = ''; $size = ''; $error = ''; 
    function compress_image($source_url, $destination_url, $quality) { 

     $info = getimagesize($source_url); 

      if ($info['mime'] == 'image/jpeg') 
      $image = imagecreatefromjpeg($source_url); 

      elseif ($info['mime'] == 'image/gif') 
      $image = imagecreatefromgif($source_url); 

      elseif ($info['mime'] == 'image/png') 
      $image = imagecreatefrompng($source_url); 

      imagejpeg($image, $destination_url, $quality); 
      return $destination_url; 
     } 
+0

感謝您的回答。我已經做到了.. –

+0

爲什麼downvote?我的代碼不相關嗎? –