I tried to store my image file in nginx server. However, the file_put_content keep return me false. I have tried to set the chmod 777 but It is still not worked so I don't think it is a permission issue.
This is my php code how I tried to store the file on server and data in database.
<?php
require("dbConnect.php");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (!empty($_POST)) {
$query = "UPDATE user SET name = :name ,
username = :username,
password = :password,
email = :email,
user_image = :user_image WHERE id = :id";
$image = $_POST['user_image'];
$path = "profile/".uniqid().".jpeg";
if(!empty($image)){
$actualpath = "https://tradeal.comp.nus.edu.sg/$path";
}else{
$actualpath = "";
}
$query_params = array(
':id' => $_POST['id'],
':name' => $_POST['name'],
':username' => $_POST['username'],
':password' => $_POST['password'],
':email' => $_POST['email'],
':user_image' => $actualpath
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error2. Please Try Again!";
die(json_encode($response));
}
if(!empty($image)){
$f=file_put_contents($path ,base64_decode($image));
}
$response["success"] = 1;
if($f===false){
$response["message"] = $f;
}else{
$response["message"] = "User Successfully Updated!";
}
$response["image"] = $actualpath;
die(json_encode($response));
}
?>
The image will store in tradeal.comp.nus.edu.sg/profile if file_put_content worked right. The data is successfully uploaded to database but the image is not store in the server.