Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何导出sqlite数据库并将其保存在angular js中的本地设备上?_Php_Angularjs - Fatal编程技术网

Php 如何导出sqlite数据库并将其保存在angular js中的本地设备上?

Php 如何导出sqlite数据库并将其保存在angular js中的本地设备上?,php,angularjs,Php,Angularjs,我正在我的应用程序中开发导出功能。目前,我正在使用下面的代码将sqlite数据导出为sql文件。在这里,我导出一个函数,其中所有数据库都被提取并发送到php代码以生成sql文件,然后保存 controller.js代码: $scope.export = function () { console.log("inside export function"); //Get user id,unit id,user t

我正在我的应用程序中开发导出功能。目前,我正在使用下面的代码将sqlite数据导出为sql文件。在这里,我导出一个函数,其中所有数据库都被提取并发送到php代码以生成sql文件,然后保存

controller.js代码:

$scope.export = function () {
                console.log("inside export function");
                //Get user id,unit id,user type id
                var unit_details = "SELECT * FROM user";
                $cordovaSQLite.execute(db, unit_details, []).then(function (res) {

                    if (res.rows.length > 0) {

                        for (var i = 0; i < res.rows.length; i++) {
                            console.log("unit_details" + res.rows.item(i).units_w_id + " unit name " + res.rows.item(i).units_name);

                            $localStorage.export_userid = res.rows.item(i).user_id;
                            $localStorage.export_unitid = res.rows.item(i).unit_id;
                            $localStorage.export_usertype_id = res.rows.item(i).user_type_id;
                            console.log("localStorage.export_usertype_id" + $localStorage.export_usertype_id + " localStorage.export_unitid " + $localStorage.export_unitid);
                        }
                    }
                }, function (err) {
                    console.log("No followupdata found frommob1");
                    $ionicLoading.hide();
                    //alert("Error ="+err);
                    console.error(err);
                });
                
                var successFn = function (sql, count) {
                    console.log("Exported SQL: " + sql);

                    var filename = $localStorage.export_unitid + "_" + $localStorage.export_usertype_id + "_" + Date.now() + '.sql';
                    console.log("filename" + filename);
                    /*-------------------Transfer file----------------*/
                    $http({
                        method: "post",
                        url: $localStorage.weburl + "parse.php",
                        params: {
                            action: "add"
                        },
                        data: {
                            filename: filename,
                            exportdata: sql,
                            usertype: $localStorage.export_usertype_id,
                            unitid: $localStorage.export_unitid
                        },
                        responseType: "json"
                    }).then(function mySucces(response) {
                        console.log("export " + JSON.stringify(response));
                        window.plugins.toast.showWithOptions({
                            message: "Database file is exported Successfully",
                            duration: "short", // 2000 ms
                            position: "center",
                            styling: {
                                opacity: 0.75, // 0.0 (transparent) to 1.0 (opaque). Default 0.8
                                backgroundColor: '#38d151', // make sure you use #RRGGBB. Default #333333
                                textColor: '#FFFFFF', // Ditto. Default #FFFFFF
                                textSize: 20.5, // Default is approx. 13.
                                cornerRadius: 20, // minimum is 0 (square). iOS default 20, Android default 100
                                horizontalPadding: 20, // iOS default 16, Android default 50
                                verticalPadding: 16 // iOS default 12, Android default 30
                            }
                        });
                    }, function myError(err) {
                            console.log("ERROR in export" + JSON.stringify(err));
                            alert("Unable to export!");
                    });
                };


                cordova.plugins.sqlitePorter.exportDbToSql(db, {
                    successFn: successFn
                });

            }
这个过程运行得很好,但我不想使用PHP部分。 如何直接生成文件并使用angular保存

  $conn = open_conn();
   // Retrieve posted data
   $posted        = file_get_contents("php://input");

   // Decode from JSON
   $obj           =  json_decode($posted);

   // Retrieve the file name and string data
   $fileName      =  strip_tags($obj->filename);
   $fileData      =  strip_tags($obj->exportdata);
   $usertypeid      =  strip_tags($obj->usertype);
   $unitid      =  strip_tags($obj->unitid);



   try {

      // Open a file stream/pointer
      $handler       = fopen('document_uploads/backupdatabase/' . $fileName, "a+");


      // Attempt to write the string data to the specified file pointer
      if(!fwrite($handler, $fileData))
      {
         echo json_encode(array('message' => 'Error! The supplied data was NOT written to ' . $fileName));
      }

      // If all has gone well attempt to close the file stream
      if(!fclose($handler))
      {
         echo json_encode(array('message' => 'Error! The file was not closed properly!'));
      }

        $save_saving = array(
            
            'unit_id' => $unitid,
            'user_type_id' => $usertypeid,
            'filename' => $fileName
            
            
            );
             $columns = implode(", ",array_keys($save_saving));
                //$escaped_values = array_map('mysql_real_escape_string', array_values($save_saving));
                $escaped_values = array_values($save_saving);
                $values  = implode("','", $escaped_values);
                
            $sql = "INSERT INTO backupfiles ($columns) VALUES ('$values')";
                $query = $conn->query($sql);
                $saving_id = $conn->insert_id;

      // If we get this far the operation has succeeded - let the user know :)
      echo json_encode(array('message' => 'The file ' . $fileName . ' was successfully uploaded'));

   }
   catch(Exception $e)
   {
      echo json_encode(array('message' => 'Fail!'));
   }