The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

uploading image and pulling image

jenia123.jen's Avatar

jenia123.jen

18 Dec, 2014 04:47 AM

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?

  1. 1 Posted by Shubham Thapa on 22 Jan, 2016 09:53 AM

    Shubham Thapa's Avatar

    $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. 2 Posted by Luann Lawrence on 08 Jan, 2018 11:16 AM

    Luann Lawrence's Avatar

    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. 3 Posted by Luann Lawrence on 08 Jan, 2018 11:18 AM

    Luann Lawrence's Avatar

    {
        "error": true,
        "message": "Required field(s) description, photo is missing or empty"
    }
    This is the JSON responds

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

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