Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过post请求将blob图像保存到php中的mysql_Php_Mysql_Slim - Fatal编程技术网

通过post请求将blob图像保存到php中的mysql

通过post请求将blob图像保存到php中的mysql,php,mysql,slim,Php,Mysql,Slim,我使用Slim框架,我有post路由将blob图像保存到mysql数据库,但当数据库中的图像列为空时,结果正常。这是我的密码: PHP路线: $app->post('/add_film', 'uploadfile', function() use ($app) {verifyRequiredParams(array('film_id','film_description','film_name', 'film_year')); $response = array();

我使用Slim框架,我有post路由将blob图像保存到mysql数据库,但当数据库中的图像列为空时,结果正常。这是我的密码: 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']);
        }
    }
    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,则不需要移动它。相反,只需对其进行base64编码,然后存储字符串。除非您感到困惑,并且希望存储物理文件并在数据库中存储引用,否则您希望存储$_files['name'],但您的上载函数除了打印之外,不会调用db函数或返回任何内容。如果您将其存储为blob,则无需移动它。相反,只需对其进行base64编码,然后存储字符串。除非您感到困惑,并且希望存储物理文件并在数据库中存储引用,否则您希望存储$_files['name'],但您的上载函数不会调用db函数或返回任何内容,打印除外。