Bhawesh
Bhawesh

Reputation: 49

Image upload in directory using POSTMAN

In my api,I am able to store all data like username ,email ,password and also imagepath in my abc table of xyz database but the image is not appearing in my folder. When I send the data using POSTMAN , it shows 'Product has been created' and all the data stored in my database but the image doesn't appears in my folder. Its my first api and I am a beginner, can anyone help me out.....

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// get database connection
include_once '/formdatabase.php';

// instantiate product object
include_once '/formproduct.php';

$database = new Database();
$db = $database->getConnection();

$product = new Product($db);

// get posted data
$data = (object) $_POST;

// set product property values
$product->username = $data->username;
$product->email = $data->email;
$product->password = $data->password; 

//path were our avatar image will be stored
        $product->avatar = ('image/'.$_FILES['avatar']['name']);

        //make sure the file type is image
        if (preg_match("!image!",$_FILES['avatar']['type'])) {

            //copy image to images/ folder 
            if (copy($_FILES['avatar']['tmp_name'], $avatar)){



            }
            else {
                echo '{';
                    echo '"message": "File upload failed!"';
                echo '}';
            }

        }   
        else {
            echo '{';
                echo '"message": "Please only upload GIF, JPG or PNG images!"';
            echo '}';
        }



// create the product
if($product->create()){
    echo '{';
        echo '"message": "Product was created."';
    echo '}';
}

// if unable to create the product, tell the user
else{
    echo '{';
        echo '"message": "Unable to create product."';
    echo '}';
}
?>

Upvotes: 0

Views: 1959

Answers (1)

CyrielleDev
CyrielleDev

Reputation: 156

When you copy your file you use $avatar instead of $product->avatar, that's why your image doesn't copy.

It should be:

if (copy($_FILES['avatar']['tmp_name'], $product->avatar))

Hoping it helps

Upvotes: 1

Related Questions