php - How to insert picture in database? -
so far can upload picture in folder path, don't have idea how can store in database. have tried couple of examples no luck far. can me?
upload.php
<?php //turn on php error reporting error_reporting(e_all); ini_set('display_errors', 1); if ($_server['request_method'] == 'post') { $name = $_files['file']['name']; $tmpname = $_files['file']['tmp_name']; $error = $_files['file']['error']; $size = $_files['file']['size']; $ext = strtolower(pathinfo($name, pathinfo_extension)); switch ($error) { case upload_err_ok: $valid = true; //validate file extensions if ( !in_array($ext, array('jpg','jpeg','png','gif')) ) { $valid = false; $response = 'invalid file extension.'; } //validate file size if ( $size/1024/1024 > 2 ) { $valid = false; $response = 'file size exceeding maximum allowed size.'; } //upload file if ($valid) { $targetpath = dirname( __file__ ) . directory_separator. 'uploads' . directory_separator. $name; move_uploaded_file($tmpname,$targetpath); header( 'location: index.php' ) ; exit; } break; case upload_err_ini_size: $response = 'the uploaded file exceeds upload_max_filesize directive in php.ini.'; break; case upload_err_partial: $response = 'the uploaded file partially uploaded.'; break; case upload_err_no_file: $response = 'no file uploaded.'; break; case upload_err_no_tmp_dir: $response = 'missing temporary folder. introduced in php 4.3.10 , php 5.0.3.'; break; case upload_err_cant_write: $response = 'failed write file disk. introduced in php 5.1.0.'; break; default: $response = 'unknown error'; break; } echo $response; } ?>
uploadpicture.php
<?php include_once("login_check.inc"); include_once("database/connection.inc"); ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>php file uploader</title> <!-- bootstrap core css --> <link href="boostrap/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <!-- static navbar --> <div class="navbar navbar-default navbar-static-top"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="index.php">php file uploader</a> </div> </div> </div> <div class="container"> <div class="row"> <?php //scan "uploads" folder , display them accordingly $folder = "uploads"; $results = scandir('uploads'); foreach ($results $result) { if ($result === '.' or $result === '..') continue; if (is_file($folder . '/' . $result)) { echo ' <div class="col-md-3"> <div class="thumbnail"> <img src="'.$folder . '/' . $result.'" alt="..."> <div class="caption"> <p><a href="remove.php?name='.$result.'" class="btn btn-danger btn-xs" role="button">remove</a></p> </div> </div> </div>'; } } ?> </div> <div class="row"> <div class="col-lg-12"> <form class="well" action="upload.php" method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="file">select file upload</label> <input type="file" name="file"> <p class="help-block">only jpg,jpeg,png , gif file maximum size of 1 mb allowed.</p> </div> <input type="submit" class="btn btn-lg btn-primary" value="upload"> </form> </div> </div> </div> <!-- /container --> </body> </html>
you can save images database in multiple ways. tend base64 encode image , save extension. don't know if practical method though.
// encoding image $images = file_get_cntents('image.jpg'); $encodedimage = base64_encode($image);
Comments
Post a Comment