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!

sonam gupta
  • 775
  • 6
  • 17
Sagar S
  • 41
  • 1
  • 6
  • 1
    Try searching ;) http://stackoverflow.com/questions/24632118/php-image-blob-wont-show http://stackoverflow.com/questions/907361/show-image-from-blob-mysql – vhu Jun 30 '15 at 07: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. – Sagar S Jun 30 '15 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? – Sagar S Jul 01 '15 at 03:56
  • I tried using this code to solve the problem but still can't seem to fix it. What am I doing wrong? `code` – Sagar S Jul 07 '15 at 05: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. – Kyle Wiering Oct 31 '15 at 15:47

0 Answers0