使用PHP在Cordova的XAMPP服务器中保存图像

使用PHP在Cordova的XAMPP服务器中保存图像,php,cordova,Php,Cordova,我的要求是保存从Cordova插件相机拍摄的图像,并将图像保存到服务器中。我使用了下面的代码并获得了图像,但是如何使用PHP保存在服务器中 // Code to capture photo from camera and show gps co ordinates <!DOCTYPE html> <html> <head> <title>Capture Photo</title> <script type="text/javas

我的要求是保存从Cordova插件相机拍摄的图像,并将图像保存到服务器中。我使用了下面的代码并获得了图像,但是如何使用PHP保存在服务器中

// Code to capture photo from camera and show gps co ordinates 
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

var pictureSource;   // picture source
var destinationType; // sets the format of returned value
// Wait for device API libraries to load
document.addEventListener("deviceready",onDeviceReady,false);
// device APIs are available
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64-encoded image data
// console.log(imageData);
// Get image handle
//
var smallImage = document.getElementById('smallImage');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The in-line CSS rules are used to resize the image
//
 smallImage.src = "data:image/jpeg;base64," + imageData;
 }
 // Called when a photo is successfully retrieved
 //
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
 // Get image handle
 //
var largeImage = document.getElementById('largeImage');
// Unhide image elements
 //
 largeImage.style.display = 'block';
 // Show the captured photo
 // The in-line CSS rules are used to resize the image
 //
 largeImage.src = imageURI;
 }
// A button will call this function
//
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded   string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.DATA_URL });
}
// A button will call this function
//
function capturePhotoEdit() {
// Take picture using device camera, allow edit, and retrieve image as     base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20,      allowEdit: true,
destinationType: destinationType.DATA_URL });
}
// A button will call this function
//
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
 // button to capture photo  
<button onclick="capturePhoto();">Capture Photo</button> <br>
// button to capture editable photo
<button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
//button to select images from library
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo  Library</button><br>
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo  Album</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>
//从照相机捕获照片并显示gps坐标的代码
拍摄照片
var pictureSource;//图像源
var destinationType;//设置返回值的格式
//等待加载设备API库
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//设备API可用
函数ondevicerady(){
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.destinationType;
}
//成功检索照片时调用
//
函数onPhotoDataSuccess(imageData){
//取消注释以查看base64编码的图像数据
//控制台日志(imageData);
//获取图像句柄
//
var smallImage=document.getElementById('smallImage');
//取消隐藏图像元素
//
smallImage.style.display='block';
//显示捕获的照片
//内嵌CSS规则用于调整图像大小
//
smallImage.src=“数据:图像/jpeg;base64,”+imageData;
}
//成功检索照片时调用
//
函数onPhotoURISuccess(imageURI){
//取消注释以查看图像文件URI
//log(imageURI);
//获取图像句柄
//
var largeImage=document.getElementById('largeImage');
//取消隐藏图像元素
//
largeImage.style.display='block';
//显示捕获的照片
//内嵌CSS规则用于调整图像大小
//
largeImage.src=imageURI;
}
//一个按钮将调用此函数
//
函数capturePhoto(){
//使用设备摄像头拍照,并将图像作为base64编码字符串检索
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{质量:50,
destinationType:destinationType.DATA_URL});
}
//一个按钮将调用此函数
//
函数capturePhotoEdit(){
//使用设备照相机拍照,允许编辑,并以base64编码字符串的形式检索图像
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{quality:20,allowEdit:true,
destinationType:destinationType.DATA_URL});
}
//一个按钮将调用此函数
//
函数getPhoto(源代码){
//从指定源检索图像文件位置
navigator.camera.getPicture(onPhotoURISuccess,onFail,{质量:50,
destinationType:destinationType.FILE\u URI,
sourceType:source});
}
//如果有什么不好的事情发生了就打电话。
//
函数onFail(消息){
警报('失败原因:'+消息);
}
//按钮捕捉照片
拍摄照片
//按钮捕获可编辑的照片 捕获可编辑照片
//按钮从库中选择图像 从照片库
来自相册
PHP代码(upload.PHP):

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

唯一的方法是以Base64编码字符串的形式获取图像,将其发送回服务器,然后以编码形式保存到数据库中,或者对其进行解码,然后将其保存为服务器上的文件或数据库中的blob


我不知道具体的实现过程,因为我不是Cordova开发人员,我只是坐在一个最近因为类似的事情而大发雷霆的同事旁边。

在Cordova,我们有插件
文件传输
,用于从服务器下载/上传文件。您必须使用此插件上载从相机拍摄的图像

检查以下内容,了解如何将文件上载到服务器

function uploadfiletoserver(filename){ // where filename is the file store in device
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
    //The folder is created if doesn't exist
    fileSys.root.getDirectory( 'APP STORAGE FOLDER', {create:true, exclusive: false},
        function(directory) {
            //find the file
            directory.getFile(filename, {create: false, exclusive: false}, 
                function(file){
                    var imageURI = file.toInternalURL();
                    var options = new FileUploadOptions();
                    options.fileKey = "uploadfile";  //this is the value use to refer the file uploaded to server e.g. $_FILES['uploadfile']
                    options.fileName = file.name;

                    var ft = new FileTransfer();
                    ft.upload(imageURI, encodeURI('http://www.yourdomain.com/upload.php'), function (r) {
                        console.log("Code = " + r.responseCode);
                        console.log("Response = " + r.response);
                        console.log("Sent = " + r.bytesSent);
                    }, function (error) {
                        console.log("upload error source " + error.source);
                        console.log("upload error target " + error.target);
                    }, options);

                    console.log("upload file: "+imageURI);
                },
                function(error){
                    console.log("Error "+error);
                }
            );
        },
        resOnError);
    },
    resOnError);
}
下面是一个Javascript函数,它拾取存储在移动设备中的文件并将其发送到服务器

function uploadfiletoserver(filename){ // where filename is the file store in device
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
    //The folder is created if doesn't exist
    fileSys.root.getDirectory( 'APP STORAGE FOLDER', {create:true, exclusive: false},
        function(directory) {
            //find the file
            directory.getFile(filename, {create: false, exclusive: false}, 
                function(file){
                    var imageURI = file.toInternalURL();
                    var options = new FileUploadOptions();
                    options.fileKey = "uploadfile";  //this is the value use to refer the file uploaded to server e.g. $_FILES['uploadfile']
                    options.fileName = file.name;

                    var ft = new FileTransfer();
                    ft.upload(imageURI, encodeURI('http://www.yourdomain.com/upload.php'), function (r) {
                        console.log("Code = " + r.responseCode);
                        console.log("Response = " + r.response);
                        console.log("Sent = " + r.bytesSent);
                    }, function (error) {
                        console.log("upload error source " + error.source);
                        console.log("upload error target " + error.target);
                    }, options);

                    console.log("upload file: "+imageURI);
                },
                function(error){
                    console.log("Error "+error);
                }
            );
        },
        resOnError);
    },
    resOnError);
}
在服务器PHP文件中

<?php
    $tmpName = $_FILES['uploadfile']['tmp_name'];
    $fileName = $_FILES['uploadfile']['name'];
    $fileDestPath = 'YOUR_UPLOAD_FOLDER_IN_SERVER'.$fileName;
    if($tmpName != 'none') {
        move_uploaded_file($tmpName, $fileDestPath); 
    }
?>


尽一切办法使用文件传输插件,启发我们。-啊,我看到你上面的评论了。是的,你可以写一个插件,但那不是原生的Cordova。您假设所有Cordova开发人员都可以编写插件。不,文件传输插件是Cordova的核心插件之一,您只需使用javascript即可。我假设所有cordova开发人员都阅读了文档并了解所有可用的核心插件。我可以用上面添加的代码拍摄照片并浏览画廊图片。但是,一旦我从相机中拍照(或)从gallery浏览照片并按下android 5.0 nexus设备上的后退按钮,我就无法再次打开相机(或)浏览照片。我错过什么了吗?