简体   繁体   中英

Save blob image to mysql in php by post request

Im using Slim Framework and I have post route to save blob image to mysql DB, but result is going ok while image column in database is empty. Here is my code: PHP route:

 $app->post('/add_film', 'uploadfile', function() use ($app) {verifyRequiredParams(array('film_id','film_description','film_name', 'film_year'));
        $response = array();
        $film_id = $app->request()->post('film_id');
        $film_name = $app->request()->post('film_name');
        $film_description = $app->request()->post('film_description');
        $film_year = $app->request()->post('film_year');

        $db = new DbHandler();
        global $imgs;
        $main_image = file_get_contents($imgs);
        // creating new task
        $task_id = $db->createFilm($film_id, $film_name, $film_description, $film_year, $main_image);

        if ($task_id != NULL) {
            $response["error"] = false;
            $response["message"] = "Task created successfully";

            echoResponse(201, $response);
        } else {
            $response["error"] = true;
            $response["message"] = "Failed to create task. Please try again";
            echoResponse(200, $response);
        }
    });

This is image parsing function:

    function uploadfile () {
        if (!isset($_FILES['uploads'])) {
            echo "No files uploaded!!";
            return;
        }
        global $imgs;
        $imgs = array();

        $files = $_FILES['uploads'];
        $cnt = count($files['name']);

        for($i = 0 ; $i < $cnt ; $i++) {
            if ($files['error'][$i] === 0) {
                $name = uniqid('img-'.date('Ymd').'-');
                if (move_uploaded_file($files['tmp_name'][$i], 'uploads/' . $name) === true) {
                    $imgs[] = array('url' => '/uploads/' . $name, 'name' => $files['name'][$i]);
                }

            }
        }

        $imageCount = count($imgs);

        if ($imageCount == 0) {
            echo 'No files uploaded!!  <p><a href="/">Try again</a>';
            return;
        }

        $plural = ($imageCount == 1) ? '' : 's';

        foreach($imgs as $img) {
            printf('%s <img src="%s" width="50" height="50" /><br/>', $img['name'], $img['url']);
        }
    }

And this is my MYSQL function:

    public function createFilm($film_id, $film_name, $film_description, $film_year, $main_image) {

        $stmt = $this->conn->prepare("INSERT INTO films(name_ru, description, outer_id, film_year, main_image) VALUES(?, ?, ?, ?, ?)");

        $stmt->bind_param("ssiib", $film_name, $film_description, $film_id,  $film_year, $main_image);

        $result = $stmt->execute();


        $stmt->close();

        if ($result) {

       return $result;
        } else {
            // task failed to create
            return NULL;
        }
    }

Using blob is not a recommended way to store and display images. Instead of blob store image in a dedicated folder and save their path & filename is database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM