我尝试从设备相机上传图像并将其插入服务器上的文件夹中,如何修改下面的代码以将捕获的图像保存到数据库表中。
<?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!";
}
}
?>
与其将图像保存到数据库,不如只将图像的路径存储在数据库中。我没有测试这段代码,但我认为你可以这样完成它:
这是要上传图像的文件夹名称。$storeFolder = '/图像'; if (!empty($_FILES(( { $uploads_dir = __DIR__ .$storeFolder; $tmp_name = $_FILES["文件"]["tmp_name"]; $name = 时间(( 。$_FILES["文件"]["tmp_name"]; if (move_uploaded_file($tmp_name, "$uploads_dir/$name"(( { 文件已上传。如果需要,请显示一些消息。 }else { 处理错误 } }