0

I have a registration form which asks for details such as firstname, lastname, email, etc and it asks for uploading a pdf file at the end. I am not using any database, I am storing all the details in a json file using php in the server. I am able to store the data as well as the pdf file in the directory path, I cannot figure out how should I connect the pdf with the user details(i.e.how can I store the pdf so that it has a field in the json file so that when the next time user logs in, he should be able to see his pdf file).

Below is the php file to store the pdf file:

<?php
  header('Access-Control-Allow-Origin: *');
  header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
  header('Access-Control-Allow-Methods: GET, POST, PUT');

  $userId = $_POST['userId'];
  $type = $_POST['type'];
  $model = array();
  $uploaded = false;
  $found = false;
  $imgDir = 'data/transcripts/';

  $users = json_decode(file_get_contents('../data/studentdata.json'));
  switch ($type) {
    case 'cis300':
      $model = $users->cis300;
      break;

    case 'cis400':
      $model = $users->cis400;
      break;

    case 'cis500':
      $model = $users->cis500;
      break;
  }
  if (isset($_FILES["file"]["name"])) {
    foreach ($model as $key => $user) {
      if ($user->id == $userId) {
        $found = true;
        $temp = explode(".", $_FILES["file"]["name"]);
        $newfilename =  str_replace(' ', '', ($user->firstname . $user->lastname)) . '-' . date('Y-m-d-h-m-s') . '.' . end($temp);
        if (move_uploaded_file($_FILES["file"]["tmp_name"], '../data/transcripts/' . $newfilename)) {
          unlink('../' . $user->tfile);
          $uploaded = true;
          $fileName = str_replace(' ', '', ($user->firstname . $user->lastname)) . $user->year;
          $user->tfile = $imgDir . $newfilename;
          echo json_encode(array('uploaded' => $uploaded, 'user' => $user));
        } else {
          echo json_encode(array('uploaded' => $uploaded, 'user' => array()));
        }
        break;
      }
    }
    if ($found) {
      unlink('../data/studentdata.json');
      file_put_contents('../data/studentdata.json', json_encode($users, JSON_PRETTY_PRINT));
    }
    if (!$found) {
      echo json_encode(array('uploaded' => $uploaded, 'user' => array()));
    }
  }
?>

Below is the angular js file where I have get requests and upload function:

csel.controller('studentController', function (AppServices, $rootScope, $scope, Upload, $http, $routeParams, $timeout, $location) {

$scope.formdata.tfile = function () {
        if ($scope.forms.registerForm.file.$valid && $scope.formdata.file) {
            $scope.upload($scope.formdata.file);
        }
    };
    $scope.upload = function (file) {
            file.upload = Upload.upload({
                url: $rootScope.baseURL + 'php/uploadT.php',
                data: {
                    file: file,
                    'userId': $scope.formdata.id, 
                    'type': $scope.formdata.type
                },
            });

            file.upload.then(function (response) {
                $timeout(function () {
                    console.log("CAlled in Upload");
                    file.result = response.data;
                });
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
            });
        }

When I press upload the file, the file is uploaded, however the file path is stored in a different json object.

Thank you in advance!

TheUnKnown
  • 681
  • 5
  • 29
  • I see the code that uploads a JavaScript object. Where is the code that uploads the file? – georgeawg Aug 10 '18 at 04:27
  • I have not yet coded to upload the file because I don't know what path I will have to give or to connect it with the json file – TheUnKnown Aug 10 '18 at 04:28
  • Possible duplicate of [How to POST binary files with AngularJS (with upload DEMO)](https://stackoverflow.com/questions/45432354/how-to-post-binary-files-with-angularjs-with-upload-demo/45433364?s=1|52.8622#45433364). – georgeawg Aug 10 '18 at 04:29
  • I checked that before, it is using angularjs only, but I will have to use PHP to actually store the data in the json file, otherwise my server won't allow it to store the data to the json file. And even if I store binary data, how will I match it to the form fields as you can see in the code, I am storing the data according to the type that user has selected. – TheUnKnown Aug 10 '18 at 04:47
  • Both. how to handle the data using angularjs so that it can match the data with the username and php to store the data. – TheUnKnown Aug 10 '18 at 12:06
  • @georgeawg I have updated the code now, do you have any other suggestion please? – TheUnKnown Aug 17 '18 at 13:07
  • There is a huge code block inside your question. Your real problem lies only in a few instructions, and most of the code posted is completely irrelevant to the resolution of your issue. It only serves as noise, and makes it harder for those trying to answer your question. Isolate the issue, and create a [Minimal, Complete, Verifiable](https://stackoverflow.com/help/mcve) example (see the StackOverflow help center for more details). Such an example is generally only a few lines of code, like a function or a database request. – georgeawg Aug 17 '18 at 15:48
  • @georgeawg Sorted the code. – TheUnKnown Aug 27 '18 at 15:13

0 Answers0