0

I am trying to upload image using ajax on client side and on server side i am using PHP. The Problem i am facing is, i am unable to save the image in Mysql database table as a blob using PHP. I am unable to define Receive function of image.

I need help on how to define receive function and save blob image in database column.
am i missing something in my PHP code below ??

Java Script:

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript">
    $(document).ready(function (e) {
        $("#uploadForm").on('submit',(function(e) {
            e.preventDefault();
            $.ajax({
                url: "upload.php",
                type: "POST",
                data:  new FormData(this),
                contentType: false,
                cache: false,
                processData:false,
                success: function(data)
                {
                $("#targetLayer").html(data);
                },
                error: function() 
                {
                }           
           });
        }));
    });
    </script>

HTML:

    <form id="uploadForm" action="" method="post">

    <div id="uploadFormLayer">
    <label>Upload Image File:</label><br/>
    <input name="userImage" type="file" class="inputFile" />
    <input type="submit" value="Submit" class="btnSubmit" />
    </form>

PHP:

$db = mysqli_connect("xxxx","xxx","xxx","xxx"); 

$image = addslashes(is_uploaded_file($_FILES['userImage']['tmp_name']));

    $query = "INSERT INTO images (id,image) VALUES('','$image')";  

    $qry = mysqli_query($db, $query);

2 Answers 2

0

Try this...

file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.

Add 'enctype="multipart/form-data"' in your form

<form id="uploadForm" action="" method="post" enctype="multipart/form-data">

 $query = "INSERT INTO images (id,image) VALUES('','" . mysql_escape_string(file_get_contents($image)) . "')";  
Sign up to request clarification or add additional context in comments.

1 Comment

Warning: file_get_contents(1): failed to open stream: No such file or directory in C:\inetpub\wwwroot\php_ajax_image_upload\upload.php on line 5
0
// Detect if POST
if($_SERVER["REQUEST_METHOD"] == "POST"){
  // Check $_FILES array
  if(isset($_FILES["inputFile"])){

    // Find location of uploaded file
    $tmpName = $_FILES["inputFile"]["tmp_name"];

    // Read data
    $fileHandle = fopen($tmpName, "r");
    $image = fread($fileHandle, filesize($tmpName));
    fclose($fileHandle);

    // Run query
    $db = mysqli_connect("xxxx","xxx","xxx","xxx"); 
    $query = "INSERT INTO images (id,image) VALUES('','$image')";  
    $qry = mysqli_query($db, $query);
  }
}

Please remember to add your own security regarding file size and type. Here is the PHP file upload documentation.

Also, not sure why you pass an empty string into the id column in your table. If this field has auto_increment (such as with the SERIAL data type), then you can just leave that field blank in the SQL:

INSERT INTO images (image) VALUES('$image');

5 Comments

Thank s For the reply
Error is not showing up now, But image is also not saving..but Was expecting the same answer as u have posted..
Check the value of $_FILES["inputFile"]["error"]. If it is not zero (success), then read the documentation on error codes.
TK didn't work nothing is saving in db nor any error is showing up :(
Then you may want to check the documentation for mysqli_connect and mysqli_query to see how to print out errors from those functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.