I'm new to php and I am using that to get my image to my computer from my app using flutter. When I open that php file on web, it says this error
Warning: file_put_contents(C: mpp\htdocslutter_testelse value): failed to open stream: No such file or directory in C:\xampp\htdocs\flutter_test\upload_image.php on line 9
So there is an error on line 9 and here is the php code
<?php
$image = isset($_POST['image']) ? $_POST['image'] : "else value";
$name = isset($_POST['name']) ? $_POST['name'] : "else value";
$realImage = base64_decode($image);
// THIS IS LINE 9:
file_put_contents("C:\xampp\htdocs\flutter_test".$name, $realImage);
echo "Image Uploaded Greatly";
?>
And here is my flutter code for the app if necessary
import 'package:flutter/material.dart';
import 'dart:io';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:image_picker/image_picker.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Upload Image Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
//visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: UploadImageDemo(),
);
}
}
class UploadImageDemo extends StatefulWidget {
UploadImageDemo() : super();
final String title = "Upload Image Demo";
@override
UploadImageDemoState createState() => UploadImageDemoState();
}
class UploadImageDemoState extends State<UploadImageDemo> {
//
static final String uploadEndPoint =
'http://localhost/flutter_test/upload_image.php';
Future<File> file;
String status = '';
String base64Image;
File tmpFile;
String errMessage = 'Error Uploading Image';
chooseImage() {
setState(() {
file = ImagePicker.pickImage(source: ImageSource.gallery);
});
setStatus('');
}
setStatus(String message) {
setState(() {
status = message;
});
}
startUpload() {
setStatus('Uploading Image...');
if (null == tmpFile) {
setStatus(errMessage);
return;
}
String fileName = tmpFile.path.split('/').last;
upload(fileName);
//print("MOIKKULI "+ fileName);
}
upload(String fileName) {
http.post(uploadEndPoint, body: {
"image": base64Image,
"name": fileName,
}).then((result) {
setStatus(result.statusCode == 200 ? result.body : errMessage);
}).catchError((error) {
setStatus(error);
});
}
Widget showImage() {
return FutureBuilder<File>(
future: file,
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
null != snapshot.data) {
tmpFile = snapshot.data;
base64Image = base64Encode(snapshot.data.readAsBytesSync());
return Flexible(
child: Image.file(
snapshot.data,
fit: BoxFit.fill,
),
);
} else if (null != snapshot.error) {
return const Text(
'Error Picking Image',
textAlign: TextAlign.center,
);
} else {
return const Text(
'No Image Selected',
textAlign: TextAlign.center,
);
}
},
);
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
print(size);
return Scaffold(
appBar: AppBar(
title: Text("Upload Image Demo"),
),
body: Container(
padding: EdgeInsets.all(30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
OutlineButton(
onPressed: chooseImage,
child: Text('Choose Image'),
),
SizedBox(
height: 20.0,
),
showImage(),
SizedBox(
height: 20.0,
),
OutlineButton(
onPressed: startUpload,
child: Text('Upload Image'),
),
SizedBox(
height: 20.0,
),
Text(
status,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.w500,
fontSize: 20.0,
),
),
SizedBox(
height: 20.0,
),
],
),
),
);
}
I am using a XAMPP apache server for this project. How do I fix this php error?
The back slash \ is an "escape character" - it gives a different meaning to the character that follows it. That means when you write "C:\xampp\htdocs\flutter_test"
what you get is something else.
One solution is to use single quotes instead of double quotes: 'C:\xampp\htdocs\flutter_test'
. Inside single quotes, \x
and \f
don't have any special meaning, they stand for themselves.
Another solution is to use the escape charater to escape itself: "C:\\xampp\\htdocs\\flutter_test"
A third solution is to use the portable directory separator character /
instead of windows-only \: "C:/xampp/htdocs/flutter_test"