2016-06-23 29 views
0

私はこのスクリプトを使用して、自分のフォームと共に自分のデータベースに画像をアップロードしようとしています。問題は、クエリに$coverを含めると、$insertの値がfalseであることです。誰かが私が間違っていることを教えてもらえますか?MySQLデータベースに画像をアップロード

<?php 
    session_start(); 
    $con = mysqli_connect("localhost", "root", "", "testdb") or die("Error " . mysqli_error($con)); 

    $title = ""; 
    $year = ""; 
    $director = ""; 
    $genre = ""; 
    $duration = ""; 
    $description = ""; 
    $name = ""; 

    $error = false; 

    //check if form is submitted 
    if (isset($_POST['addmovie'])) { 
     $title = mysqli_real_escape_string($con, $_POST['title']); 
     $year = mysqli_real_escape_string($con, $_POST['year']); 
     $director = mysqli_real_escape_string($con, $_POST['director']); 
     $genre = mysqli_real_escape_string($con, $_POST['genre']); 
     $duration = mysqli_real_escape_string($con, $_POST['duration']); 
     $description = mysqli_real_escape_string($con, $_POST['description']); 
     $file = mysqli_real_escape_string($con, $_FILES['cover']['tmp_name']); 

    //name can contain only alpha characters and space 
    if (!preg_match("/^[a-zA-Z ]+$/",$title)) { 
     $error = true; 
     $title_error = "Name input must contain only alphabets and space"; 
    } 
    if (!preg_match('/^[0-9]+$/',$year)) { 
     $error = true; 
     $year_error = "Year input must be only numbers"; 
    } 
    if (!preg_match("/^[a-zA-Z ]+$/",$director)) { 
     $error = true; 
     $director_error = "Director input must contain only alphabets and space"; 
    } 
    if(!preg_match("/^[a-zA-Z ]+$/",$genre)) { 
     $error = true; 
     $genre_error = "Genre input must contain only alphabets"; 
    } 
    if(!preg_match('/^[0-9]+$/',$duration)) { 
     $error = true; 
     $duration_error = "Duration input must be only numbers"; 
    } 
    if(!preg_match("/^[a-zA-Z ]+$/",$description)) { 
     $error = true; 
     $description_error = "Description input must be only letter and numbers"; 
    } 

    if(!isset($file)) { 
     $error = true; 
     $cover_error = "Please select an image"; 
    }else{ 
     $cover = file_get_contents($_FILES['cover']['tmp_name']); 
     $cover_name = $_FILES['cover']['name']; 
     $cover_size = getimagesize($_FILES['cover']['tmp_name']); 

     if($cover_size == false){ 
      $error = true; 
      $cover_error = "that's not an image"; 
     } 
    } 

    if (!$error) { 
     if($insert = mysqli_query($con,"INSERT INTO movies(title,d,director,genre,duration,description,cover,cover_name) VALUES('$title','$year','$director','$genre','$duration','$description','$cover','$cover_name')")) { 
      $successmsg = "Movie ".$title." scuccesfully uploaded!"; 
     } else { 
      $errormsg = "Cannot upload image!"; 
     } 
    } 
} 

?> 
+0

ファイルの保存にディスクと保存ファイルパス、その他の情報... ! –

+0

'insert'クエリをエコーし​​ます。クエリを中断するいくつかのコンテンツ '$ cover'があるかもしれません。それをスキップするには、addslashes()またはそのようなタイプの機能を使用してください。 –

答えて

0

あなたが本当にMySQLのDBに直接画像を保存したい場合は、この記事はあなたを助ける必要があります。How to upload images into MySQL database using PHP code

しかし、それはあなたのFTPサーバに画像をアップロードする代わりに、一般的には簡単だ、おそらく、作成画像やアップロードと呼ばれるフォルダを作成した後、ファイル名をSQLデータベースに保存します。その後、ファイル名を取得するときに、イメージフォルダから正しいイメージを読み込むことができます。

ここであなたを助けるかもしれない別のポストです:Upload image to server and store image path in mysql database

基本的にあなたがこれを行う:

$file_path = "uploads/"; 

$file_path = $file_path . basename($_FILES['uploaded_file']['name']); 
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) { 
    // replace $host,$username,$password,$dbname with real info 
    $link=mysqli_connect($host,$username,$password,$dbname); 
    mysqli_query($link,"INSERT INTO `files` (filename,path) VALUES ('".$_FILES['uploaded_file']['tmp_name']."','".$file_path."')") or trigger_error($link->error."[ $sql]"); 
    mysqli_close($link); 
} 
+2

これは一般的なアプローチですが、dbに直接画像を保存することができます。 –

+0

ああ、私はそれを知らなかった、ありがとう – easiestripes

0

はこのようにそれをロードした後に文字形式にファイルを変換します。

$cover = base64_encode($cover); 

挿入前。また、SQL文でバインディングを使用する必要があります。

0

イメージをデータベースに直接アップロードしないでください。これはあなたのデータベースを大きくし、応答はゆっくりとします。別のディレクトリに画像をアップロードし、そのディレクトリをデータベースに保存することができます。これはあなたの目標を効率的にアーカイブする方法です。 あなたはこれを行うことができますが、バイナリとしてフィールドを選択する必要があります。このフィールドにはバイナリデータが格納されます。

関連する問題