我有一个注册表,要求提供名字、姓氏、电子邮件等详细信息,并要求在最后上传一个 pdf 文件。我没有使用任何数据库,我使用服务器中的 php 将所有详细信息存储在 json 文件中。我能够将数据以及 pdf 文件存储在目录路径中,我无法弄清楚我应该如何将 pdf 与用户详细信息连接起来(即如何存储 pdf 以便它在 json 文件中有一个字段所以当用户下次登录时,他应该能够看到他的 pdf 文件)。
下面是存储pdf文件的php文件:
<?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()));
}
}
?>
下面是我获得请求和上传功能的 Angular js 文件:
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));
});
}
当我按下上传文件时,文件被上传,但是文件路径存储在不同的 json 对象中。
先感谢您!