php 如何从 PhoneGaps File Transfer API 检索 POST 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13860405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve POST data from PhoneGaps File Transfer API
提问by Danny
I'm using Phone Gaps (Cordova 2.1) file transfer API to post an image from the users photo library to my server. The file transfer API seems to be working fine. I'm just puzzled about retrieving this information on my server.
我正在使用 Phone Gaps (Cordova 2.1) 文件传输 API 将用户照片库中的图像发布到我的服务器。文件传输 API 似乎工作正常。我只是对在我的服务器上检索这些信息感到困惑。
Ideally, what I need to do is retrieve the image then upload it to my server. However, I can't seem to retrieve any information from the file transfer?
理想情况下,我需要做的是检索图像,然后将其上传到我的服务器。但是,我似乎无法从文件传输中检索任何信息?
My JavaScript code (posting image data) is:
我的 JavaScript 代码(发布图像数据)是:
function onDeviceReady() {
// Retrieve image file location from specified source
navigator.camera.getPicture(uploadPhoto,
function(message) { alert('get picture failed'); },
{ quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
);
}
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);
}
function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
My server side code is:
我的服务器端代码是:
$paramValue = $_POST['fileKey']; //Undefined variable
$paramValue2 = $_POST['options']; //Undefined variable
$paramValue3 = $paramValue2['fileKey'] //Undefined variable
I've also tried:
我也试过:
//POST variable
$paramValue = $_POST['params'];
echo "Param Value1: " . $paramValue['value1']; //Should return "test"
I've also tried:
我也试过:
//POST variable
$paramValue = $_POST['options'];
echo "Param Value1: " . $paramValue['options']['params']['value1']; //Should return "test"
All I'm getting is undefined variable errors?
我得到的只是未定义的变量错误?
Any help would be much appreciated, thanks!
任何帮助将不胜感激,谢谢!
回答by iOSAndroidWindowsMobileAppsDev
On http://some.server.comyou can have your /var/www/directory, in this directory you need upload.php and the code in this directory should move your image to the folder
在http://some.server.com你可以有你的/var/www/目录,在这个目录中你需要 upload.php 并且这个目录中的代码应该将你的图像移动到文件夹中
/var/www/TEST/
/var/www/TEST/
<?php
print_r($_FILES);
$new_image_name = "YEAH.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/TEST/".$new_image_name);
?>
That is the only extra thing you need.
这是您唯一需要的额外东西。
Prerequisite:
先决条件:
XAMPP LAMP WAMP or MAMP on your http://some.server.com
XAMPP LAMP WAMP 或 MAMP 在您的http://some.server.com
For clarity, this is the JavaScript and HTML just to show you how my upload.php file fits in: In your head
为清楚起见,这是 JavaScript 和 HTML,只是为了向您展示我的 upload.php 文件如何适合:在您的脑海中
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
function onDeviceReady() {
console.log("device ready");
// Do cool things here...
}
function getImage() {
// Retrieve image file location from specified source
navigator.camera.getPicture(uploadPhoto, function(message) {
alert('get picture failed');
},{
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
}
);
}
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
var params = new Object();
params.value1 = "test";
params.value2 = "param";
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(imageURI, "http://some.server.com/TEST/upload.php", win, fail, options);
}
function win(r) {
console.log("Code = " + r.responseCode.toString()+"\n");
console.log("Response = " + r.response.toString()+"\n");
console.log("Sent = " + r.bytesSent.toString()+"\n");
alert("Code Slayer!!!");
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
}
</script>
</head>
and this is what I have in my body
这就是我身体里的东西
<button onclick="getImage();">Upload a Photo</button>
回答by Will
One additional thing that is often overlooked: This section on the client js file:
经常被忽视的另一件事:客户端js文件的这一部分:
options.fileKey="file";
Must match this part on the server side:
必须在服务器端匹配这部分:
$_FILES["file"]
Otherwise, you'll get an error that would not point to this fact. This may sound obvious for some, but it could save others an hour or two of pulling hairs.
否则,您将收到一个不会指向此事实的错误。这对某些人来说可能听起来很明显,但它可以为其他人节省一两个小时的拉头发时间。
回答by Yogesh Malpani
To access parameter values access them as POST directly. For eg
要访问参数值,请直接作为 POST 访问它们。例如
Server side : echo "Param Value1: " . $_POST['value1']; //will return "test" as output.
服务器端 : echo "Param Value1: " . $_POST['value1']; //will return "test" as output.

