html - PHP and mysqli: Upload blob to database using form -
i'm trying upload image database blob using php mysqli. if seek insert image name using query, works fine , new row name created (blob null). 1 time start trying upload blob via form database using query , jquery functions, new row created, blob shows 0 b. first bit of code html form. sec php file called 1 time form submitted. suggestions appreciated. in advance.
clarification: understand uploading image directory more efficient way store it. in interests of understanding how utilize other options, trying figure out how using blob works.
edit
just clarify tried next snippet of code.
if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) { echo "import of photo success"; $aimage = file_get_contents($tmpfile); if (!($stmt=$mysqli->prepare("insert 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); } }
end edit
error messages
import of photo success notice: undefined index: aimage in /nfs/stak/students/m/martalic/public_html/cs494/test/addactorinfo.php on line 43
warning: file_get_contents() [function.file-get-contents]: filename cannot empty in /nfs/stak/students/m/martalic/public_html/cs494/test/addactorinfo.php on line 43 1 row inserted
html form
<!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 info <?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 connect 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 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 contents"; } if (!$stmt->execute()) { echo "execute failed: (" . $stmt->errno . ") " . $stmt->error; } else { printf("%d row inserted.<br/>", $stmt->affected_rows); } } else { echo "image must under 1 mb"; } $stmt->close(); } $mysqli->close(); ?> </body> </html>
have not considered uploading image do, placing in folder /uploadedimg/ , referencing filename (randomly generated) , inserting db? blob not efficient.
so upload. name file. save file. reference file in db imgname = myfile123.png , when need utilize
$myvar = $sqlquery; <img src="/uploadedimg/". $myvar ." " alt="my image" />
php html mysqli blob
No comments:
Post a Comment