1

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.

phoon
  • 369
  • 1
  • 6
  • 21
  • Is there an error? Check the nginx error log. – Spencer Nov 06 '17 at 13:56
  • @Spencer I am new to nginx, can you teach me how to check nginx error log? – phoon Nov 06 '17 at 14:01
  • @Spencer I think this is the error 2017/11/06 22:03:05 [error] 32116#0: *5441 directory index of "/usr/share/nginx/html/profile/" is forbidden, client: 137.132.84.141, server: tradeal.comp.nus.edu.sg, request: "HEAD /profile/ HTTP/1.1", host: "tradeal.comp.nus.edu.sg" – phoon Nov 06 '17 at 14:05
  • You'll need to grant permission to the user nginx is using for that particular host to access that directory, then. That will depend on how nginx is configured, though. – Spencer Nov 06 '17 at 14:27
  • @Spencer can you show me how to do that? – phoon Nov 06 '17 at 14:28
  • What kind of host are you using? – Spencer Nov 06 '17 at 14:29
  • @Spencer I used Nginx server – phoon Nov 06 '17 at 14:49

0 Answers0