我想把这个Laravel的代码转换成纯PHP。我正试图使用图像拾取器包上传多个图像。我的前端是在Flutter和后端是在PHP。上传图像到数据库,我希望这方面的一些帮助。我得到了Laravel的代码作为参考。我想把它转换成简单的PHP。
Code here
更新:我试图上传多张图片到数据库。我解决了相同的使用以下代码。
class AddFlatGallery extends StatefulWidget {
const AddFlatGallery({Key? key}) : super(key: key);
@override
State<AddFlatGallery> createState() => _AddFlatGalleryState();
}
class _AddFlatGalleryState extends State<AddFlatGallery> {
String flatname = '';
String buildingname = '';
String flatlocation = '';
Future CheckFlat() async {
SharedPreferences preferencesaddflat =
await SharedPreferences.getInstance();
setState(() {
flatname = preferencesaddflat.getString('flatname') ?? "Not
found";
buildingname =
preferencesaddflat.getString('buildingname') ?? "Not
found";
flatlocation =
preferencesaddflat.getString('flatlocation') ?? "Not
found";
});
}
late List<Asset> images = [];
Dio dio = Dio();
Widget buildGridView() {
return GridView.count(
crossAxisCount: 3,
crossAxisSpacing: 7,
children: List.generate(images.length, (index) {
Asset asset = images[index];
return AssetThumb(
asset: asset,
width: 300,
height: 300,
);
}));
}
Future<void> loadAssets() async {
List<Asset> resultList = [];
try {
resultList = await MultiImagePicker.pickImages(
maxImages: 300,
enableCamera: true,
selectedAssets: images,
cupertinoOptions: const CupertinoOptions(takePhotoIcon:
"chat"),
materialOptions: const MaterialOptions(
actionBarColor: "#abcdef",
actionBarTitle: "TenantPay",
allViewTitle: "All Photos",
useDetailsView: false,
selectCircleStrokeColor: "#000000",
));
} on Exception catch (e) {
print(e.toString());
}
if (!mounted) return;
if (mounted) {
setState(() {
images = resultList;
});
}
}
final Timer _timer = Timer.periodic(const Duration(seconds:
5), ((timer) {}));
@override
void dispose() {
super.dispose();
_timer.cancel(); //cancel the timer here
}
_saveImage() async {
if (images != null) {
for (var i = 0; i < images.length; i++) {
ByteData byteData = await images[i].getByteData();
List<int> imageData = byteData.buffer.asUint8List();
MultipartFile multipartFile =
MultipartFile.fromBytes(imageData,
filename: images[i].name, contentType:
MediaType('image', 'jpg'));
FormData formData = FormData.fromMap({
"image": multipartFile,
"flat": flatname,
"building": buildingname,
"location": flatlocation,
});
var response =
await dio.post(ApiConstant.flatuploadurl, data:
formData);
if (response.statusCode == 200) {
print(response.data);
Fluttertoast.showToast(
msg: "Images are added successfully",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 3,
backgroundColor: Colors.green,
textColor: Colors.white,
fontSize: 16.0);
Navigator.of(context).pop();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const Home(),
),
);
}
}
}
}
@override
void initState() {
super.initState();
CheckFlat();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: const Color.fromARGB(255, 30, 64, 122),
title: const Text(
'Upload Images',
),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
FloatingActionButton(
onPressed: () {
loadAssets();
},
child: const Icon(Icons.add),
),
Expanded(child: buildGridView()),
Container(
height: 60,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: CustomButton(
text: 'Save',
color: Colors.orange,
onTap: () {
_saveImage();
},
),
),
Container(
height: 60,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: CustomButton(
text: 'Continue without adding Images',
color: Colors.blueAccent,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const Home(),
),
);
},
),
),
],
),
),
);
}
}
1条答案
按热度按时间ovfsdjhp1#
你可以在有foreach的情况下这样处理,这是一个基本的教程。
https://www.w3schools.com/php/php_file_upload.asp
而且也没有Laravel到PHP的转换