Reputation: 681
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!
Upvotes: 0
Views: 86