我尝试从设备相机上传图像并将其插入服务器上的文件夹中,如何修改下面的代码以将捕获的图像保存到数据库表中。
<?php
header('Access-Control-Allow-Origin: *');
require_once("db_connect.php");
$target_path = "image/";
if(isset($_FILES['file']))
{
$target_path = $target_path . basename( $_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path))
{
$image=basename( $_FILES["file"]["tmp_name"],".jpg"); // used to store the filename in a variable
//storind the data in your database
// $query= "INSERT INTO items VALUES ('{$image}')";
//mysqli_query($con,$query);
echo "success";
} else
{
echo $target_path;
echo "There was an error uploading the file, please try again!";
}
}
?>
答案 0 :(得分:0)
不是将图像保存到数据库,而是仅将图像的路径存储在数据库中可能更好。 我没有测试这段代码,但我认为你可以这样做:
// This is the folder name where the images are going to be uploaded. $storeFolder = '/images'; if (!empty($_FILES)) { $uploads_dir = __DIR__ . $storeFolder; $tmp_name = $_FILES["file"]["tmp_name"]; $name = time() . $_FILES["file"]["tmp_name"]; if (move_uploaded_file($tmp_name, "$uploads_dir/$name")) { //File is uploaded. Show some message if you want. }else { //Handle error } }