我正在使用Slim Framework,我有将blob图像保存到mysql DB的后期路由,但是当数据库中的image列为空时结果正常。这是我的代码: 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;
}
}
答案 0 :(得分:0)
使用blob不是存储和显示图像的推荐方法。而不是blob将图像存储在专用文件夹中并保存其路径&amp; filename是数据库。