I wanna ask how can I post an image file to the MySQL using post method? Since what I did is only upload the file pathway and not the image.
my post request:
Future addProduct() async{
var url = 'http://10.0.2.2/foodsystem/addproduct.php';
http.post(url, body: {
"productname": controllerName.text,
"productprice": controllerPrice.text,
"producttype": controllerType.text,
"product_owner": globals.userId,
"image": _image.path,
//"image": _image.path.split('/').last,
});
my PHP code:
<?php
include 'conn.php';
$product_owner = $_POST['product_owner'];
$productname = $_POST['productname'];
$productprice = $_POST['productprice'];
$producttype = $_POST['producttype'];
$image = $_POST['image'];
//$realImage = base64_decode($image);
$connect->query("INSERT INTO product (product_name, product_price, product_type, product_owner, image) VALUES
('".$productname."','".$productprice."','".$producttype."','".$product_owner."','".$image."')")
?>
my MySQL table
This is the solution that I use to upload the image to the database. Basically, I am using the image path to save into MySQL. Then the image itself I save it in the htdoc folder and it works.
Flutter code: (important library need to be imported first)
import "package:async/async.dart";
import 'package:path/path.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
Future addProduct(File imageFile) async{
// ignore: deprecated_member_use
var stream= new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var length= await imageFile.length();
var uri = Uri.parse("http://10.0.2.2/foodsystem/uploadg.php");
var request = new http.MultipartRequest("POST", uri);
var multipartFile = new http.MultipartFile("image", stream, length, filename: basename(imageFile.path));
request.files.add(multipartFile);
request.fields['productname'] = controllerName.text;
request.fields['productprice'] = controllerPrice.text;
request.fields['producttype'] = controllerType.text;
request.fields['product_owner'] = globals.restaurantId;
var respond = await request.send();
if(respond.statusCode==200){
print("Image Uploaded");
}else{
print("Upload Failed");
}
PHP code:
<?php
include 'conn.php';
$image = $_FILES['image']['name'];
$product_owner = $_POST['product_owner'];
$productname = $_POST['productname'];
$productprice = $_POST['productprice'];
$producttype = $_POST['producttype'];
$imagePath = "uploads/".$image;
move_uploaded_file($_FILES['image']['tmp_name'],$imagePath);
$connect->query("INSERT INTO product (product_name, product_price, product_type, product_owner, image) VALUES ('".$productname."','".$productprice."','".$producttype."','".$product_owner."','".$image."')");
//$connect->query("INSERT INTO product (product_name,image) VALUES ('".$productname."','".$image."')");
?>
Result in MySQL:
Result in htdoc folder:
You are actually correct about storing path of the image into MySQL instead of the actual image data. MySQL is not built to store image data.
You can send image data to the backend with the way they describe here: How to upload images and file to a server in Flutter?
Then once you receive the image data, you can store the image in a directory in the server. What you save in MySQL should only be a image path within the server or a URL to the image.
You can actually store image in MySQL . It's primitive type is BLOB. it's useful if image size is less, as image can loaded fast. If image size is large then time to load it would be large hence it's not recommended to do so. Storing image path is still a better option.
Yes, you can store images in the database, but it's not advisable in my opinion, and it's not general practice.
A general practice is to store images in directories on the file system and store references to the images in the database. e.g. path to the image,the image name, etc.. Or alternatively, you may even store images on a content delivery network (CDN) or numerous hosts across some great expanse of physical territory, and store references to access those resources in the database.
Images can get quite large, greater than 1MB. And so storing images in a database can potentially put unnecessary load on your database and the network between your database and your web server if they're on different hosts.
You'll need to save as a blob, LONGBLOB datatype in mysql will work.
CREATE TABLE 'test'.'pic' (
'idpic' INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
'caption' VARCHAR(45) NOT NULL,
'img' LONGBLOB NOT NULL,
PRIMARY KEY ('idpic')
)
its a bad practice but it can be done. Not sure if this code would scale well, though.
$data = file_get_contents($_FILES['photo']['tmp_name']);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.