Client Side Script
PhoneGap Capture Image API: Capture Image PhoneGap API
//Camera Section
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
function onDeviceReady() {}
// Called when capture operation is finished
//
function captureSuccess(mediaFiles) {
uploadFile(mediaFiles[0]);
}
// Called if something bad happens.
//
function captureError(error) {
var msg = 'An error occurred during capture: ' + error.code;
navigator.notification.alert(msg, null, 'Uh oh!');
}
// A button will call this function
//
function captureImage() {
// Launch device camera application,
// allowing user to capture only one image by {limit: 1}
navigator.device.capture.captureImage(captureSuccess, captureError, { limit: 1 });
}
// Upload files to server
function uploadFile(mediaFile) {
path = mediaFile.fullPath;
name = mediaFile.name;
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=mediaFile.name;
options.mimeType="image/jpeg";
var params = new Object();
params.fullpath = path;
params.name = name;
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload( path, "http://www.serverurl.com/image_upload",
function(result) {
//upload successful
},
function(error) {
//upload unsuccessful, error occured while upload.
},
options
);
}
Server Side Code
$new_image_name = $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], WWW_ROOT . "/products/" . $new_image_name);
Reblogged this on ITechonology.
LikeLike
What does WWW_ROOT . “/products/” stand for???
LikeLike
WWW_ROOT . “/products/” , i have used because i used CakePHP at Back-end of PhoneGap app to upload the image. you can use it normally in stead of what i have used. You can see the following sample example
Thanks
ziyed
LikeLike
Hello,
How is the Javascript communicating with the PHP code? And where do I place this code?
Do I create a PHP file and place it on my server?
Sorry, my PHP is limited only to using forms.
Thanks in advance!
LikeLike
Hello @Paul, Thanks for comment
Javascript communicate with php using Ajax request.
yes, you have to create a php file to get the ajax request on your server.
if your php have the limitation then solve that problem first to make this happen.
LikeLike
Hi ziyedbd ,
Thanks for your code..its working fine..
LikeLike
@Ramesh, Thanks for your comment.
I always try to post the perfect code by testing it before very carefully at my end
LikeLike
Hello!
my PHP file don’t received any value for $_FILES;
and function ft.upload runs correctly. this function sents image bytes and returns success alert.
I have send a file to my PHP file from a html form and this way the PHP file receives the file.
it could be a problem of my server?
LikeLike
Hi Pedro,
Thanks for your comment and sorry to late reply. I am not sure why this is happening. My code is 100% working and lots of Guy already used it. You need to trace the problem. You can take help/suggestion from web search. May be its a server issue.
thanks,
ziyed
LikeLike
Hi,
I am new to this, I’m not really suere where I’m supposed to put the upload.php file. Not in my phonegap folder, but where in my laptop?
( I have easyphp, express iis and xampp)
LikeLike
Hi, thanks for comment. First of all you seems to be very new to development, that fine & congrats for your approach.
secondly the upload.php file should be placed in any server folder as you using xampp for example and ajax request from phone application will hit that upload.php to upload the the file/image that you sent from phone. But remember where you placed the upload.php file the path must be included in you ajax url at phonegap.
Hope you get the answer.
thanks
ziyed
LikeLike
Which function should i call in your js so that when i click on upload button it capture the image than.
Also what should i write in move_upload function in php so that i can move image to D: drive of my system
LikeLike
thanks for comment , you have to use js function captureImage() to access camera API to capture image
Thanks
ziyed
LikeLike
Hi ziyedbd,
I am new to phone gap,I am not getting exactly the php code required on server can you please forward the php code which I have to put on server to make it working…….
LikeLike
Hi Amit,
I have posted the sample server code at the bottom of my post, see that.
Just connect your DB, get the ajax request at server using php script and return the reply to ajax request to complete the process.
Thanks,
Ziyed
LikeLike
Hi ziyedbd,
i want to know on from php side can we pass some data and capture in response
Thnks,
Sangram
LikeLike
Hi Sangram,
Yes, you can send and receive data between cross platform using ajax technique. Request from client side.
Thanks,
Ziyed
LikeLike
Hello ziyedbd,
Thanks for your response.I have posted this question earlier on stackoverflow..
http://stackoverflow.com/questions/33969022/in-cordova-file-transfer-upload-is-there-way-to-return-data-fro-php-back-to-java
Can you please guide me on this?
Thanks,
Sangram
LikeLike
Thanks for tutorial, I found some simple & useful article for uploading images to php server using phonegap file transfer plugin http://phonegappro.com/tutorials/upload-images-to-server-using-phonegap-file-transfer-plugin/
LikeLike
It is working on android but it gives me issue on iphone as my image can not be uploaded ,we have added all camera permissions in plist.
LikeLike