2016-11-24 10 views
1

私は自分のmysqlデータベースに画像を保存しようとしています。私はそれが賢明なことではないかもしれないが、この仕事はそれが必要です。私はPHPコードを使ってイメージを保存する方法をオンラインで見てきましたが、ガイドラインに従うたびにランダムな問題がポップアップするようです。このコードでは今使用していますが、イメージとイメージ名以外のデータベースに格納されているすべての情報を取得できました。どのようにそれを動作させるための任意の提案?アップロードされた画像がデータベースに保存されないのはなぜですか?

$connect = mysqli_connect("host", "user", "pass", "dbname"); 
if(!empty($_POST)) 
{ 
     $output = ''; 
     $title = mysqli_real_escape_string($connect, $_POST["title"]); 
     $image = addslashes($_FILES['image']['tmp_name']); 
     $name = addslashes($_FILES['image']['name']); 
     $image = file_get_contents($image); 
     $image = base64_encode($image); 
     $text2 = mysqli_real_escape_string($connect, $_POST["text2"]); 
     $text1 = mysqli_real_escape_string($connect, $_POST["text1"]);  
     $dtime = date('Y-m-d H:i:s'); 
     $query = " 
     INSERT INTO news(title, image, name, text2, text1, dtime) 
     VALUES('$title', '{$image}', '$name', '$text2', '$text1', '$dtime'); 
     "; 

私はmysql_real_escape_stringのに休息や変更にaddslashesのようにそれを作るために$の画像と$名に$接続を追加し、さまざまなオプションを試してみました。

私はそれを感謝する時間があれば、それを動作させるためにいくつかの助けが必要です!

+0

...それを試してみて、何が普通の外にあるかどうかを確認uはENCTYPE = "マルチパートを持っていますか/ form-data "をフォームに入力しますか?それをフォルダにアップロードしたくないのですか? –

+1

イメージ列のデータ型は何ですか? – aksappy

+0

私もあなたのフォームを見せてください –

答えて

0

これは、ファイルを読む前にファイルパスにスラッシュを追加しているためです。

<?php 
$connect = mysqli_connect("host", "user", "pass", "dbname"); 
if(!empty($_POST)) 
{ 
     $output = ''; 
     $title = mysqli_real_escape_string($connect, $_POST["title"]); 

     // Replace below two lines... 
     // $image = addslashes($_FILES['image']['tmp_name']); 
     // $image = file_get_contents($image); 
     // With: 
     $image = file_get_contents($_FILES['image']['tmp_name']); 
     $name = addslashes($_FILES['image']['name']); 
     $image = base64_encode($image); 
     $text2 = mysqli_real_escape_string($connect, $_POST["text2"]); 
     $text1 = mysqli_real_escape_string($connect, $_POST["text1"]);  
     $dtime = date('Y-m-d H:i:s'); 
     $query = " 
     INSERT INTO news(title, image, name, text2, text1, dtime) 
     VALUES('$title', '{$image}', '$name', '$text2', '$text1', '$dtime'); 
     "; 

また、あなたは、おそらくそれは、ストレージサイズ(Base64: What is the worst possible increase in space usage?)を増加させると、データベース内のBase64エンコードファイルを保存する必要はありません。データベースにblob(バイナリデータ)としてイメージを格納します。

これは、あなたのために働くのデバッグに次のことをしようとしなかった考える:

<?php 
// Print out the submitted files and the data. 
var_dump($_FILES); 
// Check to see if it's not empty, whether the submitted filename is actually "image", 


echo (file_exists($_FILES['image']['tmp_name']) ? 'File exists!' : 'File does not exist'), PHP_EOL; 

echo (is_readable($_FILES['image']['tmp_name']) ? 'Can read the file!' : 'Can not read the file'), PHP_EOL; 

$file = file_get_contents($_FILES['image']['tmp_name']); 

echo 'File size: ', strlen($file), ' bytes (base64: ', strlen(base64_encode($file)), ' bytes)'; 

+0

がそれを試みたが、それはまだ動作しません感謝しています。私はそれを解決するために管理しました私は問題が多分私がファイルをアップロードしたときにそれを削除したとき、私はajaxを使用していたと思う! –

+0

@MattiasMatteは、私の編集を参照してください多分それはあなたが問題 – Kalkran

+0

のソースを把握することができます..それ –

関連する問題