2017-12-03 13 views
-1

画像をアップロードしようとすると、空のフィールドになります。どうしてか分かりません。 ..私は空の画像フィールドを取得mysql longblobにアップロードする画像を取得できません

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/formdata''ではなく、 '' multipart/form-data''でなければなりません。おそらく$ usernameを宣言するhttps://stackoverflow.com/questions/35417567/uploading-an-image-to-a-mysql-database-using-a-blob – kmoser

+0

@Subhamの複製?あなたがボタンでtype = "submit"を使用しなければならないもう1つのこと –

+0

@ NarendraVerma $ usernameは私のセッションのユーザ名です。問題の編集を忘れました。

答えて

-1

は、あなたが持っているこの

をお試しください:

<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: ..

HTMLフォームを助けてくださいから変更〜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?私のコードは関係ありませんか? –

関連する問題