php - 通过发布请求将blob图像保存到php中的mysql中

标签 php mysql slim

我正在使用Slim Framework,我有发布路由将blob图像保存到mysql DB,但是当数据库中的图像列为空时,结果正常。这是我的代码:
PHP路线:

 $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);
        }
    });


这是图像解析功能:

    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']);
        }
    }


这是我的MYSQL函数:

    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;
        }
    }

最佳答案

建议不要使用Blob来存储和显示图像。而不是将blob将图像存储在专用文件夹中,并将其路径和文件名保存为数据库。

关于php - 通过发布请求将blob图像保存到php中的mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45117520/

相关文章:

php - 在 Laravel 中从数据库中检索和显示单个记录

php - 验证音频文件

php - 在 Laravel 中使用同步分离数据透视表

PHP MYSQL : search word query

php - SlimPHP - 扩展 Twig (函数)的奇怪错误

slim - slim 2.0 中的 hooks 与中间件

php - 启用 url 重写后 joomla 3.0 中的 URL 重写 YES 但给出错误

php - 使用 PHP 将 CSV 文件导入 MySQL

mysql - 如何在 Rails 3.2.x 中关闭 MySQL 严格模式

php - Slim 3 - getParsedBody() 不正确的整数值 : 'NULL'