php 错误说 "file_put_contents () failed to open stream"

php error saying "file_put_contents () failed to open stream"

我是 php 的新手,我正在使用它通过 flutter 从我的应用程序将图像传输到我的计算机。当我在网络上打开那个 php 文件时,它说这个错误

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

所以第 9 行有一个错误,这里是 php 代码

   <?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";
 
?>

如有必要,这是我为该应用程序编写的 flutter 代码

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,
            ),
          ],
        ),
      ),
    );
  }

我正在为这个项目使用 XAMPP apache 服务器。我该如何解决这个 php 错误?

反斜杠 \ 是一个 “转义字符” - 它赋予后面的字符不同的含义。这意味着当你写 "C:\xampp\htdocs\flutter_test" 时,你得到的是其他东西。

一种解决方案是使用单引号代替双引号:'C:\xampp\htdocs\flutter_test'。在单引号内,\x\f 没有任何特殊含义,它们代表它们自己。

另一种解决方案是使用转义字符来转义自身:"C:\xampp\htdocs\flutter_test"

第三种解决方案是使用 可移植目录分隔符 字符 / 而不是 windows-only \: "C:/xampp/htdocs/flutter_test"