3

I am working on a phonegap application where a user can select a picture from their phone and upload it to server.

I have successfully managed to have user upload to a file path on the server such as domain.com/uploads. The index.php file for that looks like this:

<?php
    print_r($_FILES);
    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/".$_FILES["file"]["name"]);
?>

I want to take this further, and instead of storing pictures in the folder, I wanted them to go in to SQL. This index.php file looks like:

<?php
    print_r($_FILES);

    $image= addslashes($_FILES['image']['tmp_name']);
    $name= addslashes($_FILES['image']['name']);
    $image= file_get_contents($image);
    $image= base64_encode($image);
    saveimage($name,$image);




    function saveimage($name,$image)
    {
        $con=mysql_connect("sqlserver","user","pass");
        mysql_select_db("pics",$con);
        $qry="insert into pics (name,image) values ('$name','$image')";
        $result=mysql_query($qry,$con);


    }
    ?>

My SQL table has 3 rows: id(int, a_i), name (varchar), image (longblob)

The JavaScript in my application does the following:

 function uploadImage(imageData) {
    var serverURL = "index.php";
    var options = new FileUploadOptions();
    options.FileKey = 'file'
    options.fileName = imageData.substr(imageData.lastIndexOf('/')+1);
    options.mimeType = "image/jpeg";

    var ft= new FileTransfer();
    ft.upload(imageData, serverURL, onUploadSuccess, onUploadError, options);

Once again, there is no issue with the JS file. The first index.php works fine, too.

I am learning every day, so an answer will be appreciated but an explanation will be even much more appreciated. My next steps will be displaying these uploaded pictures in JS file as thumbnails. If someone wants to give me heads on articles or tell me what I need to know I would very much appreciate it!

5
  • 1
    Try searching ;) stackoverflow.com/questions/24632118/php-image-blob-wont-show stackoverflow.com/questions/907361/show-image-from-blob-mysql Commented Jun 30, 2015 at 7:31
  • @sonam Thank you for finding those links but that doesn't answer my question. My inital concern was uploading pictures to mySQL. I have seen a lot of tutorials and read up online before I posted here asking for help. When a user selects their picture from the Photo Library, the script points them to the PHP file. I am trying to have the PHP file upload that picture in to my database where I can later on display the pictures (possibly in thumbnails) using JS (not PHP like the links you provided). I apologize if there was a miscommunication in my earlier post. Commented Jun 30, 2015 at 15:39
  • I just realized I was using FileKey='File' in JS but using 'image' in PHP. I made that change but can not seem to upload. Any suggestions? Commented Jul 1, 2015 at 3:56
  • I tried using this code to solve the problem but still can't seem to fix it. What am I doing wrong? code <?php mysql_connect ("host","user","pass") mysql_select_db("pics") $image= addslashes($_FILES['file']['tmp_name']); $name= addslashes($_FILES['file']['name']); $image= file_get_contents($image); $image= base64_encode($image); if (move_uploaded_file ($_FILES["file"]["tmp_name"], "uploads/".$_FILES["file"]["name"])) { $insert = mysql_query ("INSERT INTO pics (name,file) values ('$name','$image')"); } else "error uploading query to database"; ?> Commented Jul 7, 2015 at 5:57
  • It seems like this is a misunderstanding of client to server to db flow. JavaScript cannot pull directly from a database server. Approach would be... JavaScript calls onto a PHP script which requests the info from the database and stages it for the Javascript. Commented Oct 31, 2015 at 15:47

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.