PHP と mysqli を使用して、画像を blob としてデータベースにアップロードしようとしています。クエリを使用して画像名を挿入しようとすると、問題なく動作し、その名前の新しい行が作成されます (blob は null)。クエリと jquery 関数を使用して、フォームから BLOB をデータベースにアップロードしようとすると、新しい行が作成されますが、BLOB には 0 B が表示されます。コードの最初のビットは html フォームです。2 つ目は、フォームが送信されると呼び出される php ファイルです。任意の提案をいただければ幸いです。前もって感謝します。
明確化:画像をディレクトリにアップロードする方が効率的な保存方法であることは理解しています。他のオプションの使用方法を理解するために、ブロブの使用方法を理解しようとしています。
編集
明確にするために、次のコードスニペットも試しました。
if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) {
echo "Import of photo success";
$aimage = file_get_contents($tmpfile);
if (!($stmt=$mysqli->prepare("INSERT INTO actor_info (aname, aimage) VALUE (?,?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error;
}
if (!$stmt->bind_param("sb", $_POST['aname'], $aimage)) {
echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else {
printf("%d row inserted.<br/>", $stmt->affected_rows);
}
}
編集終了
エラー メッセージ
写真のインポート成功通知: 未定義のインデックス: aimage in /nfs/stak/students/m/martalic/public_html/CS494/Test/addActorInfo.php 43 行目
警告: file_get_contents() [function.file-get-contents]: 43 行目の /nfs/stak/students/m/martalic/public_html/CS494/Test/addActorInfo.php でファイル名を空にすることはできません 1 行が挿入されました
HTMLフォーム
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="form" method="post" action=addActorInfo.php enctype="multipart/form-data">
Actor Name: <input id="aname" type="text" name="aname" class="required" maxlength="64"><br><br>
Attach Photo: <input id="aimage" type="file" name="aimage" class="required"><br><br>
<input type="submit" name="ADD" value="ADD"/>
</form>
</body>
</html>
PHP
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
Actor Information
<?php
ini_set('display_errors', 'On');
ini_set('display_startup_errors', 'On');
error_reporting(E_ALL);
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if(isset( $_POST["ADD"]) ) {
$aname = $_POST['aname'];
$errorinfo = $_FILES["aimage"]["error"];
$filename = $_FILES["aimage"]["name"];
$tmpfile = $_FILES["aimage"]["tmp_name"];
$filesize = $_FILES["aimage"]["size"];
$filetype = $_FILES["aimage"]["type"];
if (!($filetype == "image/jpeg" && $filesize > 0)) {
echo "Import of photo failed";
}
if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) {
echo "Import of photo success";
if (!($stmt=$mysqli->prepare("INSERT INTO actor_info (aname, aimage) VALUE (?,?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error;
}
$null = NULL;
if (!$stmt->bind_param("sb", $_POST['aname'], $null)) {
echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error;
}
if (!$stmt->send_long_data(0, file_get_contents($_POST['aimage']))) {
echo "Did not get contents";
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else {
printf("%d row inserted.<br/>", $stmt->affected_rows);
}
}
else {
echo "Image must be under 1 MB";
}
$stmt->close();
}
$mysqli->close();
?>
</body>
</html>