php - Uploading Storing an Image inside a MySQL -
i trying upload , store image inside mysql, reason got broken photo when photo submitted . think there problem $image = $image->fetch_object(); when click on broken photo following error message: fatal error: call member function fetch_object() on non-object suggestion how fix
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>upload image</title> </head> <body> <form action="index.php" method="post" enctype="multipart/form-data"> file: <input type="file" name='image'/> <input type="submit" value="submit"> </form> <?php //connect database try{ $db = new mysqli('','','',''); }catch(exception $e){ echo $e->getmessage(); } if(isset($_files['image'])){ $file = $_files['image']['tmp_name'];//['tmp_name'] temporary location } if(!isset($file)){ echo 'please select image'; }else{ $image = base64_encode(file_get_contents($_files['image']['tmp_name'])); $image_name = $_files['image']['name']; $image_size = getimagesize($_files['image']['tmp_name']); if($image_size == false){ echo 'thats not image'; }else{ $insert = $db->prepare("insert store values (?, ?, ?)"); $insert->bind_param('bsi', $image, $image_name, $image_size); if(!$insert->execute()){ echo "problem uploading file"; }else{ printf("%d row inserted.\n", $insert->affected_rows); echo $lastid = $db->insert_id;//returns last id inserted echo 'image uploaded.<p/> image:<p/><img src=get.php?id=$lastid>'; } } } ?> </body> </html>
this updated code, dont error anymore image still broken getmessage(); }
$id = $_request['id'];//request because going use inside image tag $image = $db->prepare("select * store id=?"); $image->bind_param('s',$id); if($image->execute()){ $image = $image->fetch(); } $image = $image->image; header('content-type: image/jpeg'); echo $image; ?>
sorry, missed main point.
the following code can't work:
$image = $db->query('select * store id=$id'); $image = $image->fetch_object();
$id in first statement not evaluated, because it's string in single quotes.
the cause broken images may use of addslashes() mask input, binary data. use prepared statement parameters , bind input data .
old answer
the php-manual says (http://www.php.net/manual/en/features.file-upload.post-method.php):
files will, default stored in server's default temporary directory, unless location has been given upload_tmp_dir directive in php.ini.
[...]
whatever logic, should either delete file temporary directory or move elsewhere.
[...]
you should move uploaded file move_uploaded_file(), before you're working it. , won't advocate storing of images in databases ...
Comments
Post a Comment