You must Sign In to post a response.
  • Category: ASP.NET

    How to Insert and Display Image or File in Php Using MySql Database .

    Hi Developers,

    I have one module in php.
    In my module i need to insert and display Images or Files using Php MySql database.

    I dont know php. but what can i do infront of our office order.

    i have tried lots of ideas from google i got error but i cant able to rectify it.

    So please friends if anyone know to insert and dispaly in php.

    Sorry for my Distrbence friends

    Thanks with
    Paul.S
  • #766834
    Hi
    I mention following url for learning my sql and php code


    "tutorialspoint.com/mysql/mysql-connection.htm"
    "w3schools.com/php/php_mysql_connect.asp"


    Step1

    try to install Xamp for php Development with mysql

    Xmap includes (Mysql,php)

    then you try i mention above url guide you,

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766835
    Hi

    can you refer this url for insert image and shows sample


    "simplifiedcoding.net/how-to-insert-image-in-mysql-database-in-php/"

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766841
    I am not PHP guy but you need to follow below steps
    1. connect to my sql using php
    below could be code, you can use

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";

    // Create connection
    $conn = new mysqli($servername, $username, $password);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    ?>

    2. save file in database using php
    see below link
    http://stackoverflow.com/questions/17377645/how-to-save-uploaded-image-in-database-with-php

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766850
    hi
    Paul

    try this php code




    <html>
    <head>
    <title>Insert Image</title>
    </head>
    <body>
    <form action="index.php" method="post" enctype="multipart/form-data">
    <input type="file" name="image" />
    <button>Upload</button>
    </form>
    </body>
    </html>




    How to Insert Image in MySQL Database in PHP


    <?php
    $msg = '';
    if($_SERVER['REQUEST_METHOD']=='POST'){
    $image = $_FILES['image']['tmp_name'];
    $img = file_get_contents($image);
    $con = mysqli_connect('localhost','root','','mysqldb') or die('Unable To connect');
    $sql = "insert into images (image) values(?)";

    $stmt = mysqli_prepare($con,$sql);

    mysqli_stmt_bind_param($stmt, "s",$img);
    mysqli_stmt_execute($stmt);

    $check = mysqli_stmt_affected_rows($stmt);
    if($check==1){
    $msg = 'Successfullly UPloaded';
    }else{
    $msg = 'Could not upload';
    }
    mysqli_close($con);
    }
    ?>





    How to Insert Image in MySQL Database in PHP


    <!DOCTYPE html>
    <html>
    <head>
    <title>Insert Image</title>
    </head>
    <body>
    <?php
    $msg = '';
    if($_SERVER['REQUEST_METHOD']=='POST'){
    $image = $_FILES['image']['tmp_name'];
    $img = file_get_contents($image);
    $con = mysqli_connect('localhost','root','','mysqldb') or die('Unable To connect');
    $sql = "insert into images (image) values(?)";

    $stmt = mysqli_prepare($con,$sql);

    mysqli_stmt_bind_param($stmt, "s",$img);
    mysqli_stmt_execute($stmt);

    $check = mysqli_stmt_affected_rows($stmt);
    if($check==1){
    $msg = 'Successfullly UPloaded';
    }else{
    $msg = 'Could not upload';
    }
    mysqli_close($con);
    }
    ?>
    <form action="index.php" method="post" enctype="multipart/form-data">
    <input type="file" name="image" />
    <button>Upload</button>
    </form>
    <?php
    echo $msg;
    ?>
    </body>
    </html>


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766880
    I do n't know PHP language and I know a mysql. Here is the solution for the question

    <?php
    if(isset($_POST['btn-upload']))
    {

    $file = rand(1000,100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $folder="uploads/";

    move_uploaded_file($file_loc,$folder.$file);
    $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$file','$file_type','$file_size')";
    mysql_query($sql);
    }
    ?>

    SRI RAMA PHANI BHUSHAN KAMBHAMPATI

  • #766975
    Hi,
    First create a column in database table to store image with BLOB data type.
    Insert image code:

    file_insert.php

    <html>
    <head><title>Insertion</title></head>
    <body>
    Please select File
    <form enctype="multipart/form-data" action=
    "<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
    <input name="userfile" type="file" />
    <input type="submit" value="Submit" />
    </form>

    <?php
    echo upload();

    function upload() {
    include "file_constants.php";
    $maxsize = 99999999;
    if($_FILES['userfile']['error']==UPLOAD_ERR_OK) {
    if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    if( $_FILES['userfile']['size'] < $maxsize) {
    //if(strpos(mime_content_type($_FILES['userfile']['tmp_name']),"image")===0) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {
    $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
    mysql_connect($host, $user, $pass) OR DIE (mysql_error());
    mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error());
    $sql = "INSERT INTO Table
    (imageColumn, Column2)
    VALUES
    ('{$imgData}', '{$_FILES['userfile']['name']}');";
    mysql_query($sql) or die("Error in Query: " . mysql_error());
    $msg='Image uploaded successfully';
    }
    else
    $msg="Uploaded file is not an image.";
    }
    else {
    // if the file is not less than the maximum allowed, print an error
    $msg='File exceeds the Maximum File limit
    Maximum File limit is '.$maxsize.' bytes
    File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].
    ' bytes';
    }
    }
    else
    $msg="Error while uploading";

    }
    else {
    $msg= "Error while uploading";
    }
    return $msg;
    }
    ?>
    </body>
    </html>


    Display image code:

    file_display.php

    <?php
    include "file_constants.php";
    error_reporting(E_ALL);
    if(isset($_GET['id']) && is_numeric($_GET['id'])) {
    $link = mysql_connect("$host", "$user", "$pass")
    or die("Could not connect: " . mysql_error());
    mysql_select_db("$db") or die(mysql_error());
    $sql = "SELECT imageColumn FROM Table WHERE id=" .$_GET['id'] . ";";
    $result = mysql_query("$sql") or die("Invalid sql query: " . mysql_error());
    header("Content-type: image/jpeg");
    echo mysql_result($result, 0);
    mysql_close($link);
    }
    ?>


    Go to web browser and run this command so that you can view the image:
    http://{your_virtual_directory_path}/file_display.php?id=1

  • #766976
    I am not sure how much knowledge in PHP. If you want to develop an application in PHP, you should understand the DB connection and My SQL. If you have good idea in PHP and My SQL, you can get some ideas from the forum discussion and try to implement the concept. If you do not have that much idea you need to spent some time to understand the PHP then start implement this.

    If you have any PHP application and if you want to access that PHP application from .net front end UI. You can make simple Http client (Http Call) to that application.

    By Nathan
    Direction is important than speed


Sign In to post your comments