The photo and the data are not storing in the database. It just alerts “file uploaded successfully” but nothing really happen. Is the error in the php file or ionic ?
upload(){
ImagePicker.getPictures({
maximumImagesCount: 1,
width: 127,
height: 127,
quality: 75
}).then((results) => {
for (var i = 0; i < results.length; i++) {
var url = "http://ifund.esy.es/register.php"
var targetPath = results[i];
// File name only
var filename = targetPath.split("/").pop();
var fileTransfer = new Transfer();
var options = {
fileKey: "file",
fileName: filename,
chunkedMode: false,
mimeType: "image/jpg",
params: {'directory': 'upload', 'fileName': filename, 'username': this.username, 'password': this.password, 'first_name': this.fname, 'last_name': this.lname, 'email': this.email, 'address': this.address, 'contact': this.contact, 'bday': this.bday}
};
fileTransfer.upload(url, targetPath, options).then((results) => {
alert('file uploaded successfully ' + results);
}, error => {
alert('server error');
});
}
}, (err) => {
let alert = this.alert.create({
title:'Warning',
subTitle: "ERROR",
buttons: ['OK']
})
alert.present(); });
}
My PHP file code
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
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
}
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);
}
$location = $_POST['directory'];
$uploadfile = $_POST['fileName'];
$uploadfilename = $_FILES['file']['tmp_name'];
$username = $_POST['username'];
$password = $_POST['password'];
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$email = $_POST['emaail'];
$contact = $_POST['contact'];
$address = $_POST['address'];
$bday = $_POST['bday'];
$type = pathinfo($uploadfile, PATHINFO_EXTENSION);
$all_types = array('jpg', 'png', 'pcd', 'fpx', 'jpeg', 'gif', 'tif', 'tiff', 'jif', 'jifi', 'jp2', 'jpx', 'j2k', 'j2c');
$type = strtolower($type);
if (in_array($type, $all_types)) {
echo 'correct type';
if (move_uploaded_file($uploadfilename, $location . '/' . $uploadfile)) {
$imagedata = file_get_contents($location . '/' . $uploadfile);
$base64 = base64_encode($imagedata);
unlink($location . '/' . $uploadfile);
$image = 'data:image/' . $type . ';base64,' . $base64;
$link = mysql_connect($config['host'], $config['username'], $config['password']);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db($config['database'], $link);
$q = "insert into register (username, password, first_name, last_name, email, contact, address, bday, permit_photo) values ('$username', '$password', '$fname', '$lname', '$email', '$contact', '$address', '$bday', '$image')";
$result = mysql_query($q);
echo $result;
} else {
echo 'Upload error!' . 'location: ' . $location;
}
} else {
echo 'wrong type file';
}
?>