1

Somethings not working, either the file upload part or the recieving end on PHP. However the record does get created in the database and the users id does get sent through. And on selecting or taking an image it does show inside the HTML img tag

Here is my PHP

<?php

    //http://stackoverflow.com/questions/18382740/cors-not-working-php
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }
    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
        exit(0);
    }
    //http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined
    $postdata = file_get_contents("php://input");
    if (isset($postdata)) {
        $request = json_decode($postdata);
        $text = $request->textpost;
        $uid = $request->uid;

        $usid = $_POST['uid'];
        $txtpos = $_POST['textpost'];
        $target_file = basename($_FILES["file"]["tmp_name"]);
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
    
  
      $mysqli = new mysqli('127.0.0.1','root','***','pdo_ret');  
      
      // If a connection error occurred, stop here.
      if ($mysqli->connect_error) {  
          die('Could not connect to Socialnetwk DB: ' . $mysqli->error);  
      }

          $conn = new mysqli('127.0.0.1','root','***','pdo_ret');

      // Switch to the ajax_demo schema
          $mysqli->select_db("ajax_demo");

            $date = date('Y-m-d') ."\n";
            $now = time(); $utc_time = $now - intval(date('Z', $now));
            $time = date('H:i:s', $now);
            $post_id=(uniqid());
            $UsrID = $usid;
            $format = 'img';
            $file_format = 'jpg';
            $MediaTxt = $txtpos;
            $author_id = $usid;
            $relation = 'feed';
            $userID = $usid;

            $MediaTxtNw = strip_tags($MediaTxt, '<h1><h2><h3><h4><h5><h6><img><video><audio><iframe><a>');

            $uploadfilename = $_FILES['file']['tmp_name'];
 
            if(move_uploaded_file($uploadfile)){
                    echo 'File successfully uploaded!';
            } else {
                    echo 'Upload error!';
            }
            $target_dir = ('media');
            $target_file = strtolower($target_dir . $post_id . $author_id .'.'.pathinfo($_FILES["file"]["tmp_name"], PATHINFO_EXTENSION));
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file));

            $sql = "INSERT INTO media (date,time,userID,post_id,format,file_format,MediaTxt,author_id,relation) VALUES ('$date', '$time', '$userID', '$post_id', '$format', '$file_format', '$MediaTxt', '$author_id', '$relation')";

            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }

          $conn->close(); 



        
     }
    else {
        echo "Error";
    }
?>

Here is my Controller

$scope.submit = function() {
     var url = "https://app.socialnetwk.com/feed_post.php";
     //target path may be local or url
     var targetPath = $scope.imgURI;
      var filename = targetPath.split("/").pop();
        var options = {
            fileKey: "file",
            fileName: filename,
            chunkedMode: false,
            mimeType: "image/jpg",
            params : {'uid':$localStorage.session_id, 'textpost':$scope.fposttxt.text}
        };
        $cordovaFileTransfer.upload(url, targetPath, options).then(function(result) {
            console.log("SUCCESS: " + JSON.stringify(result.response));
            alert("success");
            alert(JSON.stringify(result.response));
        }, function(err) {
            console.log("ERROR: " + JSON.stringify(err));
            alert(JSON.stringify(err));
        }, function (progress) {
            // constant progress updates
            $timeout(function () {
            $scope.downloadProgress = (progress.loaded / progress.total) * 100;
          })
        });
    }

Here is my HTML

<form ng-submit="submit()" id="search-form4" class="list" style="padding-top: 0px;">
  <div class="list" style="padding-top: 0px;">
    <div class="item item-input-inset" style="border:0px;">
      <label class="item-input-wrapper">
        <input type="text" ng-model="fposttxt.text" placeholder="Post" style="padding:0px;">
      </label>
         <i style="color: #387ef5;background:none;margin:0px;padding-right:2px;height:auto;min-width:15px;padding-left:2px;" class="button icon-left ion-camera" ng-model="fposttxt.image" ng-click="loadImage()" ></i>
      <img  ng-src="{{imgURI}}" style="width: 50px; height: 50px;">
      <button ng-click="uploadImage()" class="button button-small button-positive">
        Submit
      </button>
      
    </div>
  </div>
</form>
Casper Round
  • 343
  • 2
  • 14

0 Answers0