gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:44:44 25 4
gpt4 key购买 nike

我正在使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com