uploading image and pulling image
i been trying to make image uploading along with other text data and to put image in one folder that have to be reference with one id field of database.
These are my codes
$app->post('/ProjectBook', function() use ($app) {
verifyRequiredParams(array('flat_no', 'customer_name', 'status','photoid'));
$response = array();
$flat_no = $app->request->post("flat_no");
$customer_name = $app->request->post('customer_name');
$photoid = $app->request->post('photoid');
move_uploaded_file($_FILES["file"]["temp_name"], 'folders'.$photoid.'/'.$_FILES["file"]["name"]);
$data = array($flat_no, $customer_name, $status, $photoid);
$db = getConnection();
$stmt = $db->prepare("INSERT INTO projectbook(flat_no, customer_name, status, photoid ) VALUES( ?, ?, ?, ?)");
$res = $stmt->execute($data);
if ($res != NULL) {
$response["error"] = false;
$response["message"] = "submited successfully";
echoRespnse(201, $response);
} else {
$response["error"] = true;
$response["message"] = "Failed to insert data. Please try again";
echoRespnse(200, $response);
}
});
And i am using curl to test it
$curl -X POST -F "photoid=@pic.php" -F "flat_no=4&customer_name=yes&status=booked" http://localhost/Api_service/ProjectBook
but it doesn't work. Return my validation api of {"error":true,"message":"Required field(s) customer_name, status, photoid is missing or empty...."}
How can i perform this process?
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by Shubham Thapa on 22 Jan, 2016 09:53 AM
$app->post('/ProjectBook', function() use ($app)
{
verifyRequiredParams(array('flat_no', 'customer_name', 'status','photoid'));
$response = array();
$flat_no = $app->request->post("flat_no");
$customer_name = $app->request->post('customer_name');
$photoid = $app->request->post('photoid');
$status = $app->request->post('$status');
$suffix = createRandomID();
//assigning an image name..
$path1 = dirname(__DIR__);
$image_name = $path1."/uploads/"." img". $suffix . "_" . date("Y-m-d-H-m-s") . ".jpg";
$path = "uploads/"."img".$suffix."_".date("Y-m-d-H-m-s").".jpg";
move_uploaded_file($photoid,$path);
$data = array($flat_no, $customer_name, $status, $photoid);
$db = new DbHandler();
$res = $db->book($data);
if ($res != NULL) {
$response["error"] = false;
$response["message"] = "submited successfully";
echoRespnse(201, $response);
}
else if($res === false)
{
$response["error"] = true;
$response["message"] = "user already exists";
echoRespnse(200, $response);
}
else
{
$response["error"] = true;
$response["message"] = "Failed to insert data. Please try again";
echoRespnse(200, $response);
}
});
//function in DbHandler.php
public function book($data)
{
$stmt = $this->conn->prepare("SELECT * from projectbook WHERE customer_name = ?");
$stmt->execute(array($data[1]));
$rest = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($rest);
if ($rest)
{
return false;
}
else
{
$stmt = $this->conn->prepare("INSERT INTO projectbook(flat_no, customer_name, status, photoid ) VALUES( ?, ?, ?, ?)");
$res = $stmt->execute($data);
if ($res)
return
$res;
else {
return NULL;
}
}
}
2 Posted by Luann Lawrence on 08 Jan, 2018 11:16 AM
i tried using your code but is not working, i get an error in my json saying that missing credentials
public file in slim
$app->post('/activities', function(Request $request, Response $response)
{
if (isTheseParametersAvailable(array('description', 'activity_name', 'activity_date', 'photo'))) {
$response = array();
$description = $app->request->post("description");
$activity_name = $app->request->post('activity_name');
$activity_date = $app->request->post('activity_date');
$photo = $app->request->post('photo');
$suffix = createRandomID();
//assigning an image name..
$path1 = dirname(__DIR__);
$image_name = $path1."/uploads/"." img". $suffix . "_" . date("Y-m-d-H-m-s") . ".jpg";
$path = "uploads/"."img".$suffix."_".date("Y-m-d-H-m-s").".jpg";
move_uploaded_file($photo,$path);
$data = array($description, $activity_name, $activity_date, $photo);
$db = new DbOperation();
$res = $db->registerActivities($data);
if ($res != NULL) {
$response["error"] = false;
$response["message"] = "submited successfully";
echoRespnse(201, $response);
}
else if($res === false)
{
$response["error"] = true;
$response["message"] = "activity already exists";
echoRespnse(200, $response);
}
else
{
$response["error"] = true;
$response["message"] = "Failed to insert data. Please try again";
echoRespnse(200, $response);
}
}
});
DBOperator
public function registerActivities($data)
{
$stmt = $this->conn->prepare("SELECT * from activities WHERE activity_name = ?");
$stmt->execute(array($data[1]));
$rest = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($rest);
if ($rest)
{
return false;
}
else
{
$stmt = $this->conn->prepare("INSERT INTO activities(description, activity_name, activity_date, photo) VALUES( ?, ?, ?, ?)");
$res = $stmt->execute($data);
if ($res)
return
$res;
else {
return NULL;
}
}
}
3 Posted by Luann Lawrence on 08 Jan, 2018 11:18 AM
{
"error": true,
"message": "Required field(s) description, photo is missing or empty"
}
This is the JSON responds
Josh Lockhart closed this discussion on 12 Feb, 2021 07:37 PM.