Page 1 of 2

image upload to remote server

Posted: Wed Feb 20, 2013 7:38 pm
by Florian

Hi everybody,

is it possible to upload an image to a remote server (e.g. to a php-script)? I would like to store the uploaded images on my own server. If it is not nativ possible, is there any known workaround for this issue?

Thanks in advance
Florian


image upload to remote server

Posted: Wed Feb 20, 2013 7:40 pm
by Maryna Brodina

Hello! Yes, you can do that


image upload to remote server

Posted: Wed Feb 20, 2013 7:50 pm
by Florian

Hi Marina,

thanks for your reply. Can I use this code (with slightly editing).

On the server-side: I would like to process the upload with $_FILES, e.g.

precode
move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)
/code/pre

precode
function upload() {

Code: Select all

 var serverUrl = 'https://api.tiggzi.com/rest/1/db/files/' + file.name; 

 $.ajax({ 
     type: "POST", 
     beforeSend: function(request) { 
         request.setRequestHeader("X-Tiggzi-Database-Id", FileDB_settings['database_id']),              
         request.setRequestHeader("X-Tiggzi-Session-Token", localStorage.getItem('_sessionToken')), 
         request.setRequestHeader("Content-Type", file.type); 

     }, 
     url: serverUrl, 
     data: file, 
     processData: false, 
     contentType: false, 
     success: function(data) { 

         // OPTIONAL, this is the file name under which the image was stored in database.... 
         // localStorage.setItem('db_file_name', file.name); 

      }, 
     error: function(data) { 
         // do something in case of an error... 
     } 
 }); 

}
/code/pre


image upload to remote server

Posted: Wed Feb 20, 2013 8:34 pm
by Maryna Brodina

Yes, if you select image through <input type ="file">

If you get img from camera it would be easier to send that img using REST service (POST request data base64 encoded). Then on server side you don't need to get that img through move_uploaded_file, just into variable. Make decode base64 and save into file.


image upload to remote server

Posted: Thu Feb 21, 2013 8:50 am
by Florian

Hi Marina,

thanks for your reply.

I followed this simple example (http://help.gotiggr.com/documentation...) to get the image directly from the camera component. Works great. However, what are the next steps? I tryed to map the "imageDataBase64" response from camera service to a local storage variable.

I also added a second button for the upload-service. The upload service has one request parameter (the local storage variable). The upload-service is configured with url (a php-script), method = post, datatype = json, content-type = json.

The php-script on the server-side looks like this:

precode
<?php
define('UPLOAD_DIR', 'upload&#47;');
$img = $_POST['fileselect'];
$img = str_replace('data:image&#47;png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR &#46; uniqid() &#46; '&#46;png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file&#46;';
?>
/code/pre

Thanks
Florian


image upload to remote server

Posted: Thu Feb 21, 2013 4:22 pm
by Maryna Brodina

In your php you wrote $POST['fileselect'], but in your app you send img with name image, so it should be $POST['image'];

Also please check whether you have rights to write to Upload folder. To test set rights for that folder to 777.


image upload to remote server

Posted: Fri Mar 01, 2013 2:35 am
by Norman Wong

file_put_contents created a file, however there have no data in the file


image upload to remote server

Posted: Fri Mar 01, 2013 2:37 am
by maxkatz

PHP scripting is really outside the scope of our support here.


image upload to remote server

Posted: Mon Oct 14, 2013 4:21 pm
by Carlos Pinto

Please excuse me, but how exactly do you call to that button? I do have a submit button but do not know how to link that to the php script on my server. thanks in advance!


image upload to remote server

Posted: Mon Oct 14, 2013 9:34 pm
by Maryna Brodina

Hello! Sorry for delay. I'll update.