2017-07-04 18 views
-6

私はLONG​​BLOBを使うべきであることを知っています。私はこのコードを書いて、私のサーバにテキストをアップロードしています。テキストの代わりに画像をアップロードするには?phpを使用してmysqlデータベースに画像をアップロードするにはどうすればよいですか?

//if(isset...) 
$connectionText = mysql_connect("localhost", "root"); 

if(!$connectionText){ 
die("Noget gik galt?". mysql_connect_error()); 
} 
mysql_select_db("textbox", $connectionText); 

$t = $_POST['tekst']; 

$sqlScript = "INSERT INTO form (tekst) VALUES ('$t')"; 

mysql_query($sqlScript, $connectionText); 
mysql_close($connectionText); 

mysqlデータベースへのパスを保存する必要があります。イメージはサーバーのコンピュータに保存する必要があります。

+2

mysql-functionsを使用しないでください。代わりにmysqliを使用することを検討してください。 – BenRoob

+5

[PHPコードを使用してMySQLデータベースに画像をアップロードする方法](https://stackoverflow.com/questions/17717506/how-to-upload-images-into-mysql-database-using-php-code) – Troyer

+2

"PHPとmySQLを使用して画像をアップロードする"の可能な複製(https://stackoverflow.com/questions/27017834/upload-image-using-php-and-mysql) –

答えて

0

イメージをデータベースに直接保存する必要はありません。表示する場合は、イメージ名をデータベースに格納してフェッチしてください。

$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
if(isset($_POST["submit"])) { 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
} 
// Check if file already exists 
if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 500000) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
&& $imageFileType != "gif") { 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
// if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     // execute your insert query here 
     $sqlScript = "INSERT INTO form (image) VALUES ('".basename($_FILES["fileToUpload"]["name"])."')"; 


    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 
+2

*まずmysql_ *関数の使用をやめてください。彼らはSQLインジェクションの脆弱です*あなたのコード –

+0

それは実際にはかなりスマートです。コードは、ファイルサイズのチェックを外して魅力的に機能しました。しかし、どのようにファイルを表示するのですか?それはちょうど[BLOB - 19 B]と言われています。 –

+0

私はmysqli;でそれを書こうとします) –

-1

代替:画像をbase64で変換して、テキストのように保存してください。

$connectionText = mysql_connect("localhost", "root"); 

if(!$connectionText){ 
die("Noget gik galt?". mysql_connect_error()); 
} 
mysql_select_db("textbox", $connectionText); 

$image_string = base64_encode(file_get_contents($myimage)); 

$sqlScript = "INSERT INTO form (images) VALUES ('$image_string')"; 

mysql_query($sqlScript, $connectionText); 
mysql_close($connectionText); 
+0

正確には私が探していたものではなく、まだ楽しいアイデア;) –

関連する問題