I am using ng-file-upload
from Github . I have successfully uploaded and added image on both target folder and database. However, I need a specific data from my client side that will be needed for my query.
Controller.js ---- sample snippet of my upload function.
$scope.upload = function (dataUrl, name) {
Upload.upload({
url: 'php/profile-pic.php',
method: 'POST',
transformRequest: angular.identity,
headers: {
'Content-Type': undefined,
'Process-Data' : false
},
data: {
file: Upload.dataUrltoBlob(dataUrl, name),
id: user[0].id
},
}).then(function (response) {
$timeout(function () {
$scope.result = response.data;
});
console.log(response);
}, function (response) {
if (response.status > 0) $scope.errorMsg = response.status
+ ': ' + response.data;
console.log(response);
}, function (evt) {
$scope.progress = parseInt(100.0 * evt.loaded / evt.total);
});
}
PHP ---- relative code on my server side
$postdata = file_get_contents("php://input");
$data = json_decode($postdata);
// $id = $data->id;
echo json_encode($data) // returns null
if(!empty($_FILES))
{
$filename = $_FILES['file']['name'];
$destination = '/../images/' . $filename;
if(move_uploaded_file( $_FILES['file']['tmp_name'] , PATH . $destination ))
{
$insertQuery = "UPDATE tblusers SET image = ".$_FILES['file']['name']." WHERE id='???'"; // ??? = the specific data that I need
Image ---- from console.log
response
The id
is what I need to get for my query.
How can I get that? file_get_contents
not working for this one, it returns null
.
I got the answer I am looking for.
In my upload function
in my controller
, I changed the key
id
into 'id'
with apostrophe:
data: {
file: Upload.dataUrltoBlob(dataUrl, name),
'id': user[0].id
}
In my php code
, I did this:
$id = $_POST['id'];
I got the idea from Github Repo Issues at the last part of the page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.